Problem Writing To File (Dev C++ Compiler 4.9.9.2)

Hello all,
I am trying to complete a summative programming for my grade 11 programming class and appear to be having some troubles when writing to file. Any help is appreciated.
Code:[code]int high_scores(string name, int points)
{
int x, y, linecount, score[10], tick;
string names[10];
fstream WJ_names, WJ_points;
WJ_names.open("WJ_Names.txt");
WJ_points.open("WJ_Points.txt");
for (x = 0; x <= 9; x++)
{
linecount = x;
WJ_points >> score[x];
WJ_points << "";
WJ_names >> names[x];
WJ_names << "";
cout << names[x] << endl;
cout << score[x] << endl;
}
y = 0;
system("Pause");
cout << "----------------------------
";
do
{
if (points > score[y])
{
score[y] = points;
names[y] = name;
tick = 1;
}
y++;
}while(tick == 0);
for (x = 0; x <= 9; x++)
{
linecount = x;
WJ_points << score[x];
WJ_names << names[x];
cout << names[x] << endl;
cout << score[x] << endl;
}
WJ_points.close();
WJ_names.close();
return(0);
}[/code]

The reason why I don't really see a problem is because wouldn't declaring the variables in "fstream" make it possible to write and read from file? The main problem is whats meant to happen is the user enters their name and the name is added to the "highscores" text file along with the "score/points". But when I try to write to file, the file is the same as it was before (Without the new name added on)

All help is appreciated. :)

Comments

  • Problem Solved:

    Found out that I have to open the file, close the file I,opened after getting the data, then open it again, but as an ifstream so that I could write stuff to the file. All is well. :)
  • You could also just open an std::fstream with mode specifiers:

    [code]
    std::fstream file;

    // Open a file for input and output via appending
    file.open("filename.ext", std::fstream::in | std::fstream::out | std::fstream::app );
    [/code]

    Regards.
    Neb
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories