Jump to content

[C#] Replace a word from a specific line in a text file

Drown

Hello,

 

For a little program that I'm making, a bunch of informations on a user are stored in the users.txt file that I created. 

What I want to do is find a specific value in the text file about a particular user, and update it to something else.

 

This is how the text is formatted in the file : 

user1, 1500, 1user2, 1700, 37user3, 100, 419

Here is the code I am using to read the users.txt file line by line : 

string line;string path = "C:\\test\\users.txt";Streamreader sr = new Streamreader(path);while ((line = sr.ReadLine()) != null){   //Do something}

After looking around on StackOverflow, I found this solution which seemed great : 

string text = File.ReadAllText("test.txt");text = text.Replace("some text", "new value");File.WriteAllText("test.txt", text);

(using this solution means that I didn't use the Streamreader like in the previous code)

 

However, the problem is that if two users have the same values (ex: 1000), it's going to replace them all throughout the file. What I want to do is update the values only if the first value (name) matches the username used to enter the program. (which is stored in memory and sent to the update function)

 

Basically, let's say that the user1 is logged in, I want his values (ex: 1500) to be updated, and not the values of user2 and user3.

 

I hope this is clear enough, if it isn't don't hesitate to ask. I'm really lost and don't know what to do.

 

Thank you!

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Could you post the code where you are writing to the text file please?

 

Most of the values are entered manually (this is only a small test program to experiment with text file), but otherwise I use this : 

Streamwriter sw = new Streamwriter(path);sw.WriteLine(username, nbPoints, nbDays);sw.Close();

This is a function that receives a username, nbPoints and nbDays as parameters. Pretty basic yeah. :P

Link to comment
Share on other sites

Link to post
Share on other sites

Assuming the file isn't large, read the entire thing then split the lines on '\n' or '\r\n' whichever you may be using.

 

Loop through the array of lines checking if each line starts with the username you are looking for. If it does, split that line on ',' to separate the fields, replace the value you want if needed and replace the old line in the array with a new one by joining each field with a comma.

 

Then if anything changed, loop through the array again and write each line back to a file.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you @JamieF and @fizzlesticks for your help, I've found another answer though that worked fine for me.

 

Here it is, for people who might be having the same problem : 

var path = @"c:\temp\test.txt";var originalLines = File.ReadAllLines(path);var updatedLines = new List<string>();foreach (var line in originalLines){    string[] infos = line.Split(',');    if (infos[0] == "user2")    {        // update value        infos[1] = (int.Parse(infos[1]) + 1).ToString();    }    updatedLines.Add(string.Join(",", infos));}File.WriteAllLines(path, updatedLines);

Credits : VinZ, from StackOverflow

 

Thank you both for your time though, it's really appreciated. :)

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you @JamieF and @fizzlesticks for your help, I've found another answer though that worked fine for me.

 

Here it is, for people who might be having the same problem : 

var path = @"c:\temp\test.txt";var originalLines = File.ReadAllLines(path);var updatedLines = new List<string>();foreach (var line in originalLines){    string[] infos = line.Split(',');    if (infos[0] == "user2")    {        // update value        infos[1] = (int.Parse(infos[1]) + 1).ToString();    }    updatedLines.Add(string.Join(",", infos));}File.WriteAllLines(path, updatedLines);

Credits : VinZ, from StackOverflow

 

Thank you both for your time though, it's really appreciated. :)

I know this is solved, and in a good way as well...but in regards to your original post though there is another way.

 

Assuming your input is well formed (always [user], [val], [val2] where the , and space are always there).  You could do something like this, for your replace all

text.Replace("user1, 1500", "user1, 2000");  This way it actually targets the user1 and still changes the value (without having to do too much processing)....just replacing the user1 with whatever your user logged in is.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×