1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Editing a text file using visual C#

Discussion in 'C#' started by uzairfarooq, Jan 13, 2009.

  1. #1
    I had a text file log.txt and want to replace lines 3,6,9,12....... with "0". Suppose I have x no of lines in file "C:/log.txt", that is written using C#, and want to edit line no's that are divisible by 3 i.e 3,6,9,12,15.....,x.
    Any Code?
    :)
    SEMrush
     
    uzairfarooq, Jan 13, 2009 IP
    SEMrush
  2. lofvenhamn

    lofvenhamn Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi there,

    Since you've written the file in C# I assume you already know how to open and write the actual text file. To find the row divisable by 3 when you loop through the lines in the file, use the modulus operator: %.

     
    lofvenhamn, Jan 13, 2009 IP
  3. uzairfarooq

    uzairfarooq Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I had written the file using C# but unable to edit the specific lines.
    Could you provide me the full code?
    Plz
    :)
     
    uzairfarooq, Jan 14, 2009 IP
  4. Duka

    Duka Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here
    
    int "line count"; //enter number of lines in txt file
    StreamWriter sw = new StreamWriter("log.txt", true);
    for (int i = 0; i < line count; i++)
    { 
       if (line count % 3 == 0)
             sw.WriteLine("0");
    }
    sw.Flush();
    sw.Close();
    
    Code (markup):
     
    Duka, Jan 18, 2009 IP
  5. uzairfarooq

    uzairfarooq Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    but it does not overwrite lines. it writes new lines.
    I want to replace every third line.
    :)
     
    uzairfarooq, Jan 21, 2009 IP
  6. andyj00

    andyj00 Active Member

    Messages:
    532
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #6
    This will work fine. It reads the contents of your file into an Arraylist, then writes it back out, missing out every 3rd row :)

    
                ArrayList al = new ArrayList();
                string filename = @"c:\log.txt";
    
                using (StreamReader sr = new StreamReader(filename))
                {
                    string line;
                    
                    while ((line = sr.ReadLine()) != null)
                    {
                        al.Add(line);
                    }
                    sr.Close();
                }
    
                using (StreamWriter sw = new StreamWriter(filename))
                {
                    int lineCount = 1;
    
                    foreach (String str in al)
                    {
                        if (lineCount % 3 == 0)
                            sw.WriteLine("0");
                        else
                            sw.WriteLine(str);
    
                        ++lineCount;
                    }
                    sw.Close();
                }
    
    Code (markup):
     
    andyj00, Jan 26, 2009 IP