使用fstream忽略C ++输出到文件

时间:2018-04-09 13:15:39

标签: c++

所以这就是问题所在,我正在尝试将一个整数保存到一个文件中,并且在它突然停止工作之前它已经工作了一段时间我正在试图找出导致这种情况发生的原因。

以下代码中有问题的行为count << counter;

int main()
{

//variable declarations, opens countFile with cursor at end of file
fstream count("countFile.dat", ios::in | ios::binary);
int counter = 0, size;

//checks if file opened successfully
if (!count.is_open())
{
    cout << "\tError cannot open file!\n\n";
    return 1;
}

//goes to end of count file
count.ignore((numeric_limits<streamsize>::max)());

//finds number of characters in the file and saves it into size
size = count.gcount();

//resets eof flag on count file stream
count.clear();

//returns cursor to beginig of file
count.seekg(0, std::ios_base::beg);

//checks if file size is 0
if (size != 0)
{
    //inputs value from file into position 0
    count >> counter;
    //loads previous state
    load(counter);
}



//runs menu function
counter = menu(counter);

//Test contents of counter
cout << counter;

//overwrites data in countFile
count << counter;

//saves table
save(counter);

return 0;
}

感谢您的回答。

0 个答案:

没有答案