counter increment in a program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DEEPAK SAHU
    New Member
    • Jul 2011
    • 2

    counter increment in a program

    First of all, here is my C# code.
    Code:
    using System;
    using System.IO;
    
    class funny
    {
        static int i;
        public static void Main()
    
        {
            
         //File.Create(@"E:\a.txt");
         //Console.WriteLine("file a.txt successfully created");
                                          File.AppendAllText(@"E:\a.txt", ++i + " hey baby\r\n");
            
        }
    }
    Now what i need is that everytime i run this program, the text "hey baby" should be written onto a.txt file.
    But I do also need that there should be a serial number for each execution of the program just before the "hey baby".
    Just like
    1 hey baby
    2 hey baby
    3 hey baby
    4 hey baby ...

    I tried so hard, but i could not hit the target, any Happy Coders to help me out please !!
  • DEEPAK SAHU
    New Member
    • Jul 2011
    • 2

    #2
    Sorry I have wrongly commented the following statement
    [CODE]
    Console.WriteLi ne("file a.txt successfully created");
    File.AppendAllT ext(@"E:\a.txt" , ++i + " hey baby\r\n");

    [\CODE]
    Its actually meant to be without comments.
    Please somebody help.

    Comment

    • Subin Ninan
      New Member
      • Sep 2010
      • 91

      #3
      Try this, (++i).ToString( ) + " hello baby"

      Comment

      • AndrewBuchan
        New Member
        • Oct 2010
        • 14

        #4
        What you need to do is read from the a.txt file before you write your next 'hey baby' line, increment the value and then write out your text.

        Comment

        • bvrwoo
          New Member
          • Aug 2011
          • 16

          #5
          Before you can append a file you must have some content (line(s)) to append.
          First, use StreamWriter/TextWriter to add you the lines and append the ones you want to changed.

          Comment

          • bvrwoo
            New Member
            • Aug 2011
            • 16

            #6
            You need to read the lines from the file first and then you need to pass in the line(s) you want to append with File.AppendAllT ext with the file name and line which is the context.

            Comment

            Working...