gpt4 book ai didi

c++ - 创建 .dat 文件 (fstream) C++

转载 作者:行者123 更新时间:2023-11-28 00:55:06 25 4
gpt4 key购买 nike

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main() {

fstream inOutGrades( "grades.dat", ios::in | ios::out | ios::binary );

if ( !inOutGrades ) {
cerr << "The file could not be opened." << endl;
exit(1);
}

return 0;
}

//Output is
The file could not be opened.

我已经在 Windows 7 和 lubuntu 上用 QT 和 Code::blocks 尝试过这个程序。它们都无法创建 grades.dat 文件。

最佳答案

删除 ios::in 并且应该创建文件。如果文件不存在,则不能指定 ios::in。但是,如果您添加 ios::trunc,您可以同时指定 ios::inios::out,因为您是从一个空文件。

关于c++ - 创建 .dat 文件 (fstream) C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11893790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com