我是 C++ 编程的新手,遇到了一个问题。我将代码块 IDE 与它附带的默认编译器一起使用。
我的问题是,当我编写这段代码时,为什么没有在我的桌面上创建一个文件?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream output;
output.open("Desktop\\demofile.txt");
cout << "Now writing data to file" << endl;
// write four names to file
output << "Bach" << endl;
output << "Beethoven" << endl;
output << "Mozart" << endl;
output << "Schubert" << endl;
output.close();
cout << "Done!" << endl;
return 0;
}
最佳答案
因为文件(您正在使用的文件)的相对路径通常从程序启动的目录开始(但 IDE 可以更改它)。
确保使用完整路径 (C:\...)
关于c++ - fstream 代码没有在我的桌面上创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31881257/