找不到在C++中创建的文件
Files created in C++ can't be found
我想使用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(但您需要自己决定;这些只是示例(。
相关文章:
- 终端不会为C++文件创建.exe文件吗
- 如何从具有两列的.txt文件创建并行数组?
- 从 .lib 文件创建一个.dll文件
- 如何从输入文件 c++ 创建随机名称生成器
- 无法从 cso 文件创建顶点着色器(从 fx 文件创建)
- 如何为多个目录中的多个文件创建生成文件?
- 在编译时从 c++ 文件创建枚举
- 如何在运行时从 c++ 中的 DLL 文件创建对象?
- 文件创建并不总是成功
- 如何使用 QFile、c++ 从文件创建整数数组
- 您可以使用 makefile 从 .o 对象文件创建 C++ 文件吗?
- 从函数中的dat文件创建一个2维数组
- 如何为 cpp 和 h 文件创建别名(并忽略编译器__attributes__)
- 如何从现有的生成文件创建 CMake 文件?
- 为什么我无法使用文本文件创建文件映射?
- 通过读取文件创建映射<字符串,矢量>时如何管理内存<string>
- 从文本文件创建 2D 字符串矢量
- ofStream: - 尽管文件创建了,但文本文件上没有输出
- 从语言文件创建JSON对象
- 将文本文件创建到节点.js的插件C++