N Cosmin

N Cosmin

  • NA
  • 2
  • 0

Modify Text in a File...

Mar 17 2006 11:00 AM

As i was reading from other forums about this problem, i didn't find any clear solutin to it.

I'm trying to read line by line a file.When the line matches a certain group of characters, i want to rewrite the whole line with another modifyied line, or to modify a certain group of characters from that line.

Here is the code that should rewrite that line:

const String FILE_NAME = @"C://my.txt";
String s = "Text to replace with";
StreamReader ad = File.OpenText(FILE_NAME);
String input = null;
while ((input = ad.ReadLine()) != null)
      {
        if (input==s)
            {/*write a code that rewrite the whole line or
                 modify a group of characters from that line*/
             }
      }


Example(if the file contains the lines below, and the line that i'm looking for, is containing the string "Text2"),

Text1
Text2
Text3

The procedure should modify the file this way:

Text1
This line was modifyied
Text3


I will apreciate if you could give me a solution, in this context.