views:

37

answers:

3

If I have a text file like:

123, joe blow, USA

Where the first values represent:

USERID, NAME, COUNTRY

If my file has 5000 rows, could I update a particular row somehow using C#?

+1  A: 

You have three options.

  1. Increase your accept rate
  2. Use a Stream and find the index of your row and insert some characters
  3. Load the entire file into an array, modify it and re-write the whole file.

With option number 2, your code will be more complex, but it will scale to work even if your file grows to 500,000,000 rows.

With option number 3, the code is very simple, but since you load the whole thing to memory it is not very efficent for large datasets.

For option three, code like this might get you started:

var sbld = new System.Text.StringBuilder(); 
var rows = System.IO.File.ReadAllLines(@"C:\YourUserTextFile.txt");
foreach(var row in rows)
{
    if(!updateThisRow(row)) 
    {
        var ar = row.Split(',');
        var id = ar[0];
        var name = ar[1];
        var country = ar[2];
        // do update here
        sbld.AppendLine(String.Format("{0},{1},{2}", id, name, country));
    }
    else
    {
        sbld.AppendLine(row);
    }
}

System.IO.File.WriteAllText(C:\YourUserTextFile.txt", sbld.ToString());

The updateThisRow method might be something you write to see its a row you care to update.

Nate Bross
+4  A: 

While it's possible to alter a line in a file - you can only do so safely if the replacing text is exactly the same length. Since this is rarely the case, your best bet is to replace the entire contents of the file with the new results.

Line-based text files are not a great format for storing information that is likely to change for exactly this reason.

Now, assuming the file is not too large, you can load it in its entirety into an array of strings, replace the text of a particular line, and the write the file back out:

using System.IO;

var linesOfText File.ReadLines( "/myfile.txt", Text.Encoding.UTF8 ).ToArray();

linesOfText[123] = "456, sally smith, USA";

File.WriteAllLines( "/myfile.txt", linesOfText );

EDIT: In the interest of brevity, I provided an example above that uses indexed position to update a particular line. In any real-world usage, you should parse the lines and search for the one you want to update/replace rather than relying on an offset.

I would not use the above code if the file is excessively large, ~5,000 lines of 50 characters is relatively small (< 1Mb) so, I wouldn't worry. But if you're going to do this type of operation over and over, you may want to rethink how you store this data.

LBushkin
+1 for >Line-based text files are not a great format for storing information that is likely to change for exactly this reason.
Nate Bross
Correct but probably worth pointing out that you're assuming that the userids are sequential and none of been deleted. Sequential is usually the case but if any have been deleted your example is wrong. There'd need to be some sort of actually searching for the right line before changing it.
McAden
+1  A: 

You have to read the entire file, update your line and then write the file again.

However, having said that you could read the file a line at a time writing each line to a new file (modifying the required line as necessary). Then delete (or rename) the original and rename the new file.

ChrisF