在linux中创建一个文件

erb*_*bal 1 c++

我是linux的新手,我想创建一个文件并写入内容.

我没有收到任何错误,但代码不会创建任何文件...我错过了什么?

#include <iostream>
#include <fstream>

int main(){
    std::ofstream out("/Home/peter/Dropbox/C++/linux/data.dat", std::ios::out | std::ios::binary);
    if(!out)
        std::cout << " File isn't open\n" << std::endl;
    char s = 'a';

    for(int i = 0; i<100; i++)
        out.put(s);

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

For*_*cke 6

您应该添加out.close()到程序的末尾.这将刷新写缓冲区以确保正确写入.

此外,确认您实际上(而不是您的程序具有)在该目录中创建和写入文件的权限.

最后,确保您写入的路径实际上是正确的.正如@Adam在评论中指出的那样,你可能想要使用/home/...而不是/Home/...