C++: Where does the ofstream class save the files to?(C++:ofstream 类将文件保存到哪里?)
问题描述
我从 Windows 迁移到 Mac,现在我遇到了文件输入/输出类的问题:ifstream &ofstream.
I moved from Windows to Mac and now I'm experiencing a problem with the file input/output classes: ifstream & ofstream.
在 Windows 中使用 g++/代码块运行时
In Windows when you run with g++/Code Blocks
ofstream out("output.txt");
out << "TEST";
out.close();
一个新文件output.txt"将在同一目录中创建.
A new file "output.txt" will be created in the same directory.
但是在 MAC OS X 中,这个文件是在我的主目录中创建的:/Users/USER_NAME/output.txt
However in MAC OS X, this file is created in my home directory: /Users/USER_NAME/output.txt
我怎样才能把这个文件和可执行文件放在同一个目录下?
How can I have this file in the same directory together with the executable?
附言我正在使用 GCC 和 CodeBlocks.没有项目 - 我只是编译一个源文件.
P.S. I'm using GCC and CodeBlocks. There are no projects - I'm just compiling a single source file.
推荐答案
流类与所有其他文件打开函数一样,在您提供相对路径时使用当前目录.您可以使用像 chdir 这样的函数来控制当前目录,但更好的解决方案是使用完全限定的文件名.然后删除程序对当前目录的依赖.
The stream classes, like all other file-opening functions, use the current directory when you provide a relative path. You can control the current directory with a function like chdir, but a better solution is to use fully qualified file names. Then you remove your program's dependency on the current directory.
这篇关于C++:ofstream 类将文件保存到哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++:ofstream 类将文件保存到哪里?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
