--- In [email protected], "glogic_1" <[EMAIL PROTECTED]> wrote:
>
> Hey all
> Im having a problem writing to text files. I am able to read
> from them no problem but just cant write to it.
> 
> struct sScore {
>         char cName[40];
>         int score;
> }TopTen[SCORES];
> 
> fstream OutFile;
> OutFile.open("HighScore.txt",ios::in);
> if (OutFile){
>      while (!OutFile.eof())
>      {
>             OutFile >> TopTen[iNumber].cName;
>             OutFile >> TopTen[iNumber].score;
>             iNumber++;
>       }
> }
>  
> The above code works fine for taking in player name and
> score no prob but the below code, when im sending an
> amended topten back out to the txt, just clears the txt
> file completely
> 
> OutFile.open("HighScore.txt",ios::out);
<snip>

Of course your code overwrites the existing text.

Why?

Because you open the text file in output mode; this means: forget
about the existing content and rewrite the file from scratch.

What you probably need (I am no C++ expert) is something like
  OutFile .open( "HighScore.txt", ios::app);
if you want to _append_ text to an existing file.

I don't know for sure whether ios::app is correct; I just browsed
through ios.h and found the constants in, out, and app there, so I
hope it's correct.

Regards,
Nico

Reply via email to