c++ - fstream 代码没有在我的桌面上创建文件

标签 c++ file-io

我是 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/

相关文章:

c - 从 c 文件中读取行并将字符串放入数组中

c++ - Visual Studio 2013 中的 Boost Spirit kwd 解析器

c++ - 构建 shell - IO 问题

c++ - 我是否正确地执行了这个程序?

c++ - 类方法 VS 类静态函数 VS 简单函数 - 性能方面?

C - 使用 .txt 文件作为输入时遇到无限循环

c++ - 将全精度 double 输出到文本文件的最佳方法

c++ - 如何将链表的内容写入文件

c++ - C++ 中的 SDL 静音音量

c++ - 部分类模板特化