Вопрос

I'm doing a little program where the data saved on some users are stored in a text file. I'm using Sytem.IO with the Streamwriter to write new information to my text file.

The text in the file is formatted like so :

name1, 1000, 387
name2, 2500, 144

... and so on. I'm using infos = line.Split(',') to return the different values into an array that is more useful for searching purposes. What I'm doing is using a While loop to search for the correct line (where the name match) and I return the number of points by using infos[1].

I'd like to modify this infos[1] value and set it to something else. I'm trying to find a way to replace a word in C# but I can't find a good way to do it. From what I've read there is no way to replace a single word, you have to rewrite the complete file.

Is there a way to delete a line completely, so that I could rewrite it at the end of the text file and not have to worried about it being duplicated?

I tried using the Replace keyword, but it didn't work. I'm a bit lost by looking at the answers proposed for similar problems, so I would really appreciate if someone could explain me what my options are.

Это было полезно?

Решение

If I understand you correctly, you can use File.ReadLines method and LINQ to accomplish this.First, get the line you want:

var line = File.ReadLines("path")
           .FirstOrDefault(x => x.StartsWith("name1 or whatever"));

if(line != null)
{
    /* change the line */
}

Then write the new line to your file excluding the old line:

var lines = File.ReadLines("path")
                .Where(x => !x.StartsWith("name1 or whatever"));

var newLines = lines.Concat(new [] { line });

File.WriteAllLines("path", newLines);

Другие советы

The concept you are looking for is called 'RandomAccess' for file reading/writing. Most of the easy-to-use I/O methods in C# are 'SequentialAccess', meaning you read a chunk or a line and move forward to the next.

However, what you want to do is possible, but you need to read some tutorials on file streams. Here is a related SO question. .NET C# - Random access in text files - no easy way?

You are probably either reading the whole file, or reading it line-for-line as part of your search. If your fields are fixed length, you can read a fixed number of bytes, keep track of the Stream.Position as you read, know how many characters you are going to read and need to replace, and then open the file for writing, move to that exact position in the stream, and write the new value.

It's a bit complex if you are new to streams. If your file is not huge, copying a file line for line can be done pretty efficiently by the System.IO library if coded correctly, so you might just follow your second suggestion which is read the file line-for-line, write it to a new Stream (memory, temp file, whatever), replace the line in question when you get to that value, and when done, replace the original.

It is most likely you are new to C# and don't realize the strings are immutable (a fancy way of saying you can't change them). You can only get new strings from modifying the old:

String MyString = "abc 123 xyz";

MyString.Replace("123", "999");  // does not work

MyString = MyString.Replace("123", "999");  // works

[Edit:]

If I understand your follow-up question, you could do this:

infos[1] = infos[1].Replace("1000", "1500");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top