Link to home
Start Free TrialLog in
Avatar of Ameerh24
Ameerh24

asked on

editing with text file c#

hi,

I want to read text file (which has some value written in one line for example "55")  in c#

now after reading, I want to check the value if its equal to 55 I want to clear this value and store new value (for example "66") and save the file with new value (where 55 is replaces with 66)

ASKER CERTIFIED SOLUTION
Avatar of Jarrod
Jarrod
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

you will have to use the StreamReader and StreamWriter classes. Check them out on MSDN.

gsx1022
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of magicdlf
magicdlf

Just want to mention that if you happened to use VS2005, change this line:
using (var sw = new StreamWriter("c:\\text.txt", false))
to this:
using (StreamWriter sw = new StreamWriter("c:\\text.txt", false))

You can also get the int value by this:
int value = int.TryParse(sr.ReadLine());

I think zadeveloper did a good job helping you. Happy Coding!
Avatar of kaufmed
All of these solutions assume that your text file only contains one line of text. Is that the case, or does your file have multiple lines of text?
read all the text from your file with File.ReadAllText method, loop into all lines of file and just call string.replace("55","66").
HTH
Or , using the StreamReader you can call ReadToEnd()
And of course, that would depend on how big your file is. I imagine you'd have a tough time processing a 4 GB text file if you called ReadToEnd().

;)
This is true, but from the description of the problem give it would seem like a 1 line file. I would not use any suggestion on this page to open a 4gb text file. :)