Problem Writing To File (Dev C++ Compiler

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.txt");"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;
cout << "----------------------------
if (points > score[y])
score[y] = points;
names[y] = name;
tick = 1;
}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;

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. :)


  • 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:

    std::fstream file;

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

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!