Ofstream使用目录,没有目录则无法工作

#include <iostream>
#include<fstream>
using namespace std;
int main() {
    ofstream myfile;
    ofstream file2;
    myfile.open("/home/me/CLionProjects/Project2/xample.txt",ios::app); //this file doesn't exist
    file2.open("testingfile.txt",ios::app); // this file also doesn't exist
    if (myfile.is_open())
    {
        myfile << "Writing this to a file.\n";
        cout<<"test"<<endl;
        myfile.close();
    }
    else cout<<"error";
    if(file2.is_open())
    {
        file2<< "Writing this to a file";
        cout<<"test2";
        myfile.close();
    }
    else cout<<"error";
return 0;

我从这段代码中得到的结果是:myfile和file2都通过了if测试,如果是myfile,则新文件xample.txt保存在我创建的目录中。但是,在我的计算机上找不到新文件“ testingfile.txt”(或者我只是找不到它)。根据此来源,应创建一个新文件:http://www.cplusplus.com/doc/tutorial/files/ 我究竟做错了什么? 编辑:操作系统:Linux Ubuntu 18.04

b02020202 回答:Ofstream使用目录,没有目录则无法工作

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3025591.html

大家都在问