找不到在C++中创建的文件

Files created in C++ can't be found

本文关键字:文件 创建 C++ 找不到      更新时间:2023-10-16

我想使用C++来创建文件,然后可以导出这些文件以在其他地方使用。下面的代码似乎可以创建文件,从某种意义上说,我可以将数据写入文件,然后稍后在C++程序中再次读取它。但是,当我尝试实际找到创建的文件以使用它时,它无处可寻。.

using namespace std;
int main () {
    ofstream myfile ("example3.dat");
    if (myfile.is_open()){
        myfile << 3335 << " " << 64  << " " << 43 <<  9 << 5 << 6 << 5 << 4 << 6;
        myfile.close();
    }
    else cout << "Unable to open file";
    ifstream myfile2 ("example3.dat");
    int b;
    myfile2 >> b; cout <<b;
    myfile2 >> b; cout <<b;
    myfile2.close();
    return 0;
}

这些文件将在进程工作目录中创建。

如果进程是从IDE(例如Visual Studio(运行的,则工作目录可以与可执行文件路径不同。您应该检查项目属性以找出实际路径。

当然,您可以按照上面的注释中的建议搜索该文件,但最好在程序中指定绝对路径,以便知道它正在写入的位置。

路径的格式是特定于操作系统的,但也许

/tmp/example.dat
C:WindowsTempexample.dat

分别适用于 Linux 和 Windows(但您需要自己决定;这些只是示例(。