开发者

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!


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.


I think the only way is to read all lines in array (or any other data structude, i.e. list), modify and write it back to file. Maybe xml will be better for your purposes?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜