6

Is it possible to replace the text in a text file with a new text without erasing the other data, here is my sample code, but its not working, I know there's a problem with it but I can't figure out, thanks,

private void button1_Click_1(object sender, EventArgs e)
{
    StreamReader sr = new StreamReader("test10101.txt");
    List<string> lines = new List<string>();
    while (!sr.EndOfStream)
        lines.Add(sr.ReadLine());
    output = Convert.ToInt32(textBox1.Text);
    newbal = Convert.ToInt32(lines[0]) - output;
    MessageBox.Show("Please get your cash....\n\nYour new balance is: $" + newbal);
    sr.Close();
    {
        string linetoreplace = lines[0];
        int newlinevalue = newbal;
        string contents = sr.ReadToEnd();

        StreamWriter sw = new StreamWriter("test10101.txt" + ".tmp");
        //contents = Regex.Replace(contents, linetoreplace, newlinevalue.ToString());
        contents = contents.Replace(linetoreplace, newlinevalue.ToString());
        sw.WriteLine(contents);
        sw.Close();

    }

I'm wondering if I use the Regex or directly replace the line,

slugster
  • 49,403
  • 14
  • 95
  • 145
Pyromancer
  • 2,429
  • 5
  • 19
  • 28
  • Please do not stack your question title with tags, it isn't necessary. – slugster Jan 26 '13 at 03:27
  • 1
    It is not working is very bad explanation of the problem. Please remove unrelated code from sample and add details about error/behavior you see. – Alexei Levenkov Jan 26 '13 at 03:29
  • The problem is, I need to make a program which uses streamwriter and streamreader, using only 1 text file, but the problem is, if I use writeline all the text file will be replaced, I need only a single line be to replace. The error there is, it does not write any data, can you help me with my problem? thanks – Pyromancer Jan 26 '13 at 03:36
  • isn't your `contents` variable empty? you're closing the streamreader then you call `ReadToEnd`, and even if you don't close it, I think the stream position will be at the end of the file at that moment since you used the same streamreader earlier to read the entire file – ppetrov Jan 26 '13 at 03:41

1 Answers1

17

You could do it a lot more easily:

        string[] lines = System.IO.File.ReadAllLines("test");
        lines[0] = /* replace with whatever you need */
        System.IO.File.WriteAllLines("test", lines);

hope this helps

also I'd suggest using int.TryParse if you don't want an exception to be raised in your portion of code in case the first line of the file or the textbox values aren't numeric

if you really want to use the streamwriter you could go with this, also a simpler way:

line[0] = newbal.ToString();
foreach(string s in lines)
    sw.WriteLine(s);
ppetrov
  • 3,077
  • 2
  • 15
  • 27
  • 1
    mmm, How can I replace it, for example in my example above, Is it possible to replace it with my newbal? – Pyromancer Jan 26 '13 at 03:43
  • just need to assign the new string value as you can see in my edit (think you posted your comment at the same time I did the edit) – ppetrov Jan 26 '13 at 03:45
  • The simple solution works however it rewrites all file, it is not optimal. Just imagine that your file is quite big. Perfect solution would be to replace just needed symbols (replace some bytes and save without rewriting others) – Sergey Kulgan Jan 19 '17 at 12:17
  • @SergeyKulgan If you have really big files in which you want to make changes at specific locations, you have to consider using a database, since it will manage all the problems you have to overcome to make that kind of changes. Implementing it by yourself is just reinventing the wheel. Just consider the case when you don't have enough space to replace the value you want, and you'll need to move the next bytes. – ppetrov Jan 23 '17 at 13:18