C++ Ofstream在文件末尾添加空行

C++ Ofstream在文件末尾添加空行,c++,file,ifstream,ofstream,C++,File,Ifstream,Ofstream,基本问题我希望有办法解决。当我在C++中用OFFROW写入一个文件时,它在末尾添加了一个空行。有没有办法阻止这种行为?当我下次运行程序时加载文件时,它会导致问题 int saveScores(scoreboard sBoard, const string filename) { ofstream outStream; outStream.open("/Users/Wescat/Desktop/Programming/comp2710/wsc0010_project1/scores

基本问题我希望有办法解决。当我在C++中用OFFROW写入一个文件时,它在末尾添加了一个空行。有没有办法阻止这种行为?当我下次运行程序时加载文件时,它会导致问题

int saveScores(scoreboard sBoard, const string filename) {
    ofstream outStream;
    outStream.open("/Users/Wescat/Desktop/Programming/comp2710/wsc0010_project1/scores.txt");
    if (outStream.fail()) {
        cout << "Output file opening failed." << endl;
        return 1;
    }
    int i = 0;
    while (i < sBoard.numOfScores) {
        outStream << sBoard.score->name << endl;
        outStream << sBoard.score->score << endl;

        sBoard.score = sBoard.score->next;
        i++;
    }
    outStream.close();
    return 0;
}
int保存分数(记分板,常量字符串文件名){
流外流;
outStream.open(“/Users/Wescat/Desktop/Programming/comp2710/wsc010_project1/scores.txt”);
if(超出流。失败()){

这是完全合理的

您解释为空行的方式就是您的文本编辑器在实际的最后一行(由您的最后一行
std::endl
生成)结尾处显示一个
\n
字符。该字符在那里是有意义的,因为它是您所有行的结尾。事实上,这就是“行结尾”意思是


如果在重新加载文件时这会导致问题,那么我建议您真正的bug实际上就在那里。

请将代码发布到流的写入位置。在您的代码中,最后写入的是
std::endl
中的换行符。