C++ 使用二进制文件进行简单的高分更新 #包括 #包括 #包括 使用名称空间std; //#定义调试 int main() { #ifndef调试 int new_高分; cout>new_highscore;//输入5 #恩迪夫 fstream文件(“bin_file.dat”,ios::binary | ios::in | ios::out);//文件已经有10 6 4 #ifdef调试 int x=0; while(file.read(reinterpret_cast(&x),sizeof(x))) cout

C++ 使用二进制文件进行简单的高分更新 #包括 #包括 #包括 使用名称空间std; //#定义调试 int main() { #ifndef调试 int new_高分; cout>new_highscore;//输入5 #恩迪夫 fstream文件(“bin_file.dat”,ios::binary | ios::in | ios::out);//文件已经有10 6 4 #ifdef调试 int x=0; while(file.read(reinterpret_cast(&x),sizeof(x))) cout,c++,binaryfiles,C++,Binaryfiles,此位显然是错误的(假设您确实希望对值进行排序): 作为: file.write(重新解释cast(scores.data()),sizeof(scores[0])*scores.size()); 一般来说,我只是将文件读入一个向量,将新值插入内存中正确的位置,然后将其写出来。唯一可能不起作用的情况是,如果您的高分表超过2-3GB,而您的操作系统/应用程序为32位。除非您确实需要这样做,否则读取整个文件要容易得多,请添加新值e(s)在向量的末尾,对向量进行排序,然后将整个内容写出来。 #incl

此位显然是错误的(假设您确实希望对值进行排序):

作为:

file.write(重新解释cast(scores.data()),sizeof(scores[0])*scores.size());

一般来说,我只是将文件读入一个向量,将新值插入内存中正确的位置,然后将其写出来。唯一可能不起作用的情况是,如果您的高分表超过2-3GB,而您的操作系统/应用程序为32位。

除非您确实需要这样做,否则读取整个文件要容易得多,请添加新值e(s)在向量的末尾,对向量进行排序,然后将整个内容写出来。
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
//#define DEBUG
int main()
{

#ifndef DEBUG
int new_highscore;
cout << "Enter your new highscore: ";
cin >> new_highscore; //input 5
#endif

fstream file("bin_file.dat", ios::binary | ios::in | ios::out); //file already had 10 6 4

#ifdef DEBUG
int x = 0;
while (file.read(reinterpret_cast<char*>(&x), sizeof(x)))
cout << x << " ";
#endif

#ifndef DEBUG
if (file.is_open())
{
    streampos pre_pos = ios::beg;
    int cur_score = 0;
    vector <int> scores;

    while (file.read(reinterpret_cast<char*>(&cur_score), sizeof(cur_score)))
    {
        if (cur_score < new_highscore)
        {
            break;
        }
        pre_pos = file.tellg();
    }

    if (file.fail() && !file.eof())
    {
        cout << "Error! Exiting..." << endl;
        return 0;
    }

    file.clear();
    file.seekg(pre_pos);

    //get all scores that lesser than new high scores into vector
    while (file.read(reinterpret_cast<char*>(&cur_score), sizeof(cur_score)))
        scores.push_back(cur_score);

    //put new high score into right position 
    //edit
    file.seekp(pre_pos);
    file.write(reinterpret_cast<char*>(&new_highscore), sizeof(new_highscore));

    //put all the scores that lesser than new high score into file
    for (vector<int>::iterator it = scores.begin(); it != scores.end(); it++)
        file.write(reinterpret_cast<char*>(&*it), sizeof(*it));
    file.clear();
}
else
    cout << "Error openning file! " << endl;

//Try to print to console the result for checking
cout << "Review:" << endl;
file.seekg(0, ios::beg);
int temp = 0;

while (file.read(reinterpret_cast<char*>(temp), sizeof (temp))) //Error here, and can't write 5 to the file
    cout << temp << endl;
#endif
file.close();
return 0;
}
//put new high score into last position in file
file.seekp(0, ios::end);
file.write(reinterpret_cast<char*>(&new_highscore), sizeof(new_highscore));
for (vector<int>::iterator it = scores.begin(); it != scores.end(); it++)
    file.write(reinterpret_cast<char*>(&*it), sizeof(*it));
file.write(reinterpret_cast<char*>(scores.data()), sizeof(scores[0]) * scores.size());