1

I'm trying to make betting program in C#, storing the user's data in a txt file. I have no problem reading the data from it. However, I can't manage to overwrite it.

From what I've tested, if I call the StreamWriter part alone the overwriting happens just fine. When I put the same code after the StreamReader part, the code will reach the Console.WriteLine("reached"); line and ignore everything after it (username is never written in the console). No error is detected and compilation won't stop either.

Here's the class code:

    class Dinero
    {
            private List<string> data;
            private string path = @"C:\Users\yy\Documents\Visual Studio 2015\Projects\ErikaBot\ErikaBot\img\bank_data.txt";

           ...
           some other methods here
           ...

            public void thing(string username, int money)
            {
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                data = new List<string>();
                using (StreamReader sr = new StreamReader(fs))
                {
                    string a = sr.ReadLine();
                    for (int i = 0; a != null; i++)
                    {
                        if (a != username)
                        {
                            data.Add(a);
                        }
                        else i++;
                        a = sr.ReadLine();
                    }
                }

                string b = Convert.ToString(money);
                Console.WriteLine("reached");
                using (StreamWriter tw = new StreamWriter(fs))
                {
                    Console.WriteLine(username);
                    if (data != null)
                    {
                        for (int i = 0; i < data.Count; i++)
                        {
                            tw.WriteLine(data.ElementAt(i));
                        }
                    }
                    string money2 = Convert.ToString(money);
                    tw.WriteLine(username);
                    tw.WriteLine(money2);
                }
            }
        }
Hammies
  • 13
  • 4
  • 1
    are you sure that `data` is not `null` means do u have data in the file. – Mohit S Nov 10 '16 at 03:18
  • Try using FileMode.Append instead of FileMode.OpenOrCreate. – Harminder Nov 10 '16 at 03:24
  • Doesn't seem like the problem since the code should reach the Console.WriteLine(username); anyway. – Hammies Nov 10 '16 at 03:28
  • Just tested the FileMode.Append thing. Now both StreamReader and StreamWriter won't work. – Hammies Nov 10 '16 at 03:33
  • 1
    You want to open file for appending, look at this answer: http://stackoverflow.com/questions/10383053/create-file-if-file-does-not-exist – Harminder Nov 10 '16 at 03:49
  • @Hammies just a side note, but you should not use a for loop for that. Check MS docs on how to use the [StreamReader](https://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx) - `while ((line = sr.ReadLine()) != null)` – Jim Nov 10 '16 at 04:50
  • 1
    @Jim Thanks for the advice. I'll edit that part of the code asap. – Hammies Nov 10 '16 at 05:45

1 Answers1

1

By disposing StreamReader you also dispose the FileStream. Either repeat the filestream initialisation before the using statement for StreamWriter or put the latter in the using statement for StreamReader.

αNerd
  • 528
  • 1
  • 6
  • 11