0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397

3 Answers3

1

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);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 1
    `ReadLines`/`WriteAllLines`...awesome additions from 4.0 that I did not even know were there. Kudos for pointing them out! – mdisibio Apr 06 '14 at 21:47
  • @mdisibio yeah they are extremely useful when working with text files. – Selman Genç Apr 06 '14 at 21:48
  • Thank you very much(and also thanks to @mdisibio), my question was how to update it in the text file and not only how to modify the value in memory. I will try to be clearer in my next questions. :) –  Apr 07 '14 at 03:14
0

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");
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • Why the down vote? This is what the OP said: "I tried using the Replace keyword, but it didn't work." – Steve Wellens Apr 06 '14 at 20:37
  • Thank you for your fast reply, if I was to use `MyString = MyString.Replace("1000", "1500")` and then set `infos[1]` to `MyString`, it could work? –  Apr 06 '14 at 20:38
  • You could even use `infos[1] = infos[1].Replace("1000", "1500");` However, are you perhaps confusing string replacement with updating the file? The string is in memory after you've read it from the file. Changing it only changes it in memory. `Replace` does not write the new value back to the file. You have to do the file write as part of your code. – mdisibio Apr 06 '14 at 21:08
0

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.

Community
  • 1
  • 1
mdisibio
  • 3,148
  • 31
  • 47