0

I am working on a simple console application which writes into .txt files. I have few Streamwriters with append set to false:

StreamWriter j1 = new StreamWriter(@"jmeno1.txt", false);
StreamWriter j2 = new StreamWriter(@"jmeno2.txt", false);
StreamWriter s1 = new StreamWriter(@"skore1.txt", false);
StreamWriter s2 = new StreamWriter(@"skore2.txt", false);
StreamWriter l1 = new StreamWriter(@"legy1.txt", false);
StreamWriter l2 = new StreamWriter(@"legy2.txt", false);

First I write down the default values:

string jmeno1;
string jmeno2;
int legy1 = 0;
int legy2 = 0;
int skore1 = 501;
int skore2 = 501;
Console.WriteLine("První jméno?");
jmeno1 = Console.ReadLine();
Console.WriteLine("Druhé jméno?");
jmeno2 = Console.ReadLine();

j1.WriteLine(jmeno1);
j2.WriteLine(jmeno2);
s1.WriteLine(skore1.ToString());
s2.WriteLine(skore2.ToString());
l1.WriteLine(legy1.ToString());
l2.WriteLine(legy2.ToString());
j1.Flush();
j2.Flush();
s1.Flush();
s2.Flush();
l1.Flush();
l2.Flush();

Then after some user inputs I want to overwrite these files with new strings (using the same way as for the default ones). But the files aren't being overwritten, the text is only being appended. I find this really strange since the append is set to false. I've never experienced this before.

Here's the part of code where writing to files happens (sorry for the foreign language):

Console.WriteLine("\n" + jmeno1 + " hodil/a?");
skore1 = skore1 - int.Parse(Console.ReadLine());

if (skore1 == 0)
{
    legy1++;

    // writing to file
    l1.WriteLine(legy1.ToString());
    l1.Flush();

    Console.WriteLine("\n" + jmeno1 + " zavřel/a!");
    skore1 = 501;                                       
    skore2 = 501;

    // writing to file
    s1.WriteLine(skore1.ToString());
    s2.WriteLine(skore2.ToString());

    s1.Flush();
    s2.Flush();
    zacina = 2;
}

else
{
    // writing to file
    s1.WriteLine(skore1.ToString());
    s1.Flush();

    Console.WriteLine("\n" + jmeno2 + " hodil/a?");
    skore2 = skore2 - int.Parse(Console.ReadLine());

    if (skore2 == 0)
    {
        legy2++;

        // writing to file
        l2.WriteLine(legy2.ToString());
        l2.Flush();

        Console.WriteLine("\n" + jmeno2 + " zavřel/a!");
        skore1 = 501;
        skore2 = 501;

        // writing to file
        s1.WriteLine(skore1.ToString());
        s2.WriteLine(skore2.ToString());
        s1.Flush();
        s2.Flush();

        zacina = 2;
    }

    else
    {
        // writing to file
        s2.WriteLine(skore2.ToString());
        s2.Flush();
    }
}

The file with the score then looks like this.

Thanks for any help.

Milan Vodák
  • 125
  • 1
  • 8
  • 1
    Please provide a [mcve] - ideally with just *one* file. I strongly suspect you're misdiagnosing or misunderstanding; it really *won't* append with that code. – Jon Skeet Nov 05 '17 at 14:11
  • You mean before closing the StreamWriters you see they append? I'm glad they do, since it would be impossible to write more than one line to a file otherwise. Have you tried closing them after writing your default values and re-opening them when you want to overwrite them? – oerkelens Nov 05 '17 at 14:30
  • Yes, that's it! I forgot to close them. Thank you very much! – Milan Vodák Nov 05 '17 at 14:43

1 Answers1

0

Your code (which is incomplete at the time of writing) shows one wrong assumption about what appending mode of StreamWriter (let's call it SR) means. Once file is opened and SR writes first lines followed by SR.Flush you merely written text to file, advanced FileStream position to byte length of text written, and flushed buffer containing text to disk. Next call to SR.WriteLine will write next line in the very same file starting at last FileStrea.Position without overwriting anything.

Whereas option append to file for StreamWriter merely means that when you open the very same file next time with StreamWriter it will append text to exisitng content. In contrary using option not append creates new empty file which will overwrite any exiting content previously being present in that file. MSDN documentation on bool append constructor parameter says the following:

append Type: System.Boolean true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

Jacek Blaszczynski
  • 3,183
  • 14
  • 25