Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I'm reading a c++ book for beginners, and currently i'm learning how to deal with files, this examples should have a high scores list, the user enter a number and finally the the new high score is inserted in the list. Here's the code
C++
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    fstream file("highscores.txt",ios::in | ios::out);
    file.seekp(0);
    file.seekg(0);
    if (! file.is_open())
    {
        cout << "Could not open the file";
        return 0;
    }
    int new_high_score;
    cout << "Enter a new high score : ";
    cin >> new_high_score;
    streampos pre_score_pos = file.tellg();
    int cur_score;
    while (file >> cur_score)
    {
        if (new_high_score > cur_score)
        {
            break;
        }
        pre_score_pos = file.tellg();
    }
    if (file.fail() && !file.eof())
    {
        cout << "Some bad inputs";
        return 0;
    }
    file.clear();
    file.seekg(pre_score_pos);
    vector<int> scores;
    while (file >> cur_score)
    {
        scores.push_back(cur_score);
    }
    if (!file.eof())
    {
        cout<< "Some bad inputs";
        return 0;
    }
    file.clear();
    file.seekp(pre_score_pos);
    if (pre_score_pos != 0)
    {
        file << endl;
    }
    file << new_high_score << endl;
    for (vector<int>::iterator itr = scores.begin(); itr != scores.end(); ++itr)
    {
        file << *itr << endl;
    }
}

For example, the list is : 4 2 1 0 , the entry is 3 the list become 4 2 3 1 0.
Where is the error

thanks, Samuel.
Posted
Comments
Richard MacCutchan 18-Jun-15 7:17am    
Most of the file code (seeks and tells) looks to be redundant. All you need to do is read the file conents into your vector, add the new item at the end, sort it into whatever order you require, and write it out again. You are also calling seekp and seekg before you check if the file is open, which could lead to the program crashing.
Samuel Shokry 18-Jun-15 13:10pm    
Thanks for your help, but what this code ? why it doesn't copy items to the vector that it should be copied.
Richard MacCutchan 18-Jun-15 15:27pm    
Sorry, but this code really is not very logical; forget all the file seeking code, it does nothing useful.
First work out how to read the items from a file and add them to a vector.
Next wotk out how to do the same after reading a new value, and adding that at the correct point.
That's really all there is to it.
Samuel Shokry 18-Jun-15 16:15pm    
I'll try, Thanks for help. ^_^

1 solution

The reason it does not output as you want is in the if condition where you are comparing the cur_score with the new_high_score and applying break to the while loop, the file pointer is already past the position of cur_score in the file and is now pointing to the next character in the file. This is the position you are saving in pre_score_pos. Then you are fetching the rest of the scores into the vector, appending the new_high_score into the file at prev_score_pos (the position just after the first number smaller than your new_high_score) and then you append the rest of the sequence.
The pre_score_pos is not holding the very correct position you would want. You need to decrement it by one.
And, yes, too many unnecessary seeks and tells.
 
Share this answer
 
Comments
Samuel Shokry 1-Jul-15 14:26pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900