Search code examples
c#xnanewlinestreamreaderstreamwriter

XNA StreamReader/Writer how to read/write in different lines?


I'm trying to write a simple .txt via StreamWriter. I want it to look like this:

  • 12
  • 26
  • 100

So simple numbers. But how do I tell the Reader/writer in which line to write or read.

http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

Here it says that ReadLine() reads a line of current the Stream. But how do I know which line it is. Or is it always the first one?

I want to read the numbers, modify them and then write them back.

Any suggestions?

Thanks in advance!


Solution

  • A reader is conceptually a unidirectional thing, from the start (or more accurately, the current position in the stream) to the end.

    Each time you read a line, it is simply buffering data until it finds a new line; it doesn't really have the concept of a current line (or moving between lines).

    As long as the file isn't massive, you should be OK reading the entire file, working on a string (or string array), then saving the entire file; inserting/removing text content is otherwise non-trivial (especially when you consider the mysteries of encodings).

    File.ReadAllLines and File.WriteAllLines may be easier in your scenario.