#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream testfile;
testfile.open ("test.txt");
testfile << "success!\n";
testfile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
1)调用"g ++ testfile.cpp"
2)创建"test.txt"
3)调用"chmod u + x a.out"
4)???
5)文件保持空白.
我觉得自己是一个傻瓜,因为这应该是一件微不足道的事情.
小智 5
执行文件I/O时,您几乎总是需要测试错误:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream testfile;
testfile.open ("test.txt");
if ( ! testfile.is_open() ) {
cerr << "file open failed\n";
return 1;
}
if ( ! testfile << "success!\n" ) {
cerr << "write failed\b";
return 1;
}
testfile.close(); // failure unlikely!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |