c++ - 文件流打不开

标签 c++ c++11 ofstream

我有以下 C++ 代码作为更大程序的一部分:

/* Open the output streams */
    std::ofstream outputFile;
    outputFile.open(outputName);
    std::ofstream outputFile1;
    outputFile1.open(outputName1);
    std::ofstream outputFile2;
    outputFile2.open(outputName2);

    std::cout <<  outputFile.is_open() << " " << outputFile1.is_open() << " " << outputFile2.is_open() << std::endl;
if (inputFile.is_open() && outputFile.is_open() && outputFile1.is_open() && 

outputFile2.is_open())
...

它应该打开几个输出文件流,然后 - 如果它们都打开 - 执行一系列写入每个文件的操作。然而,程序提前终止,因为流从未打开:

0 0 0
Filestream or output streams could not open, ending program!

变量 outputName 是 std::strings,我认为它在 C++11 中是允许的(我在我的 OpenBlocks 编译器选项中为 C++11 启用了 -std 标志)。

我不确定为什么无法打开流。

谢谢。

最佳答案

文件流可能由于多种原因而无法打开,例如路径无效、访问被拒绝或文件已打开以供在另一个程序中写入。

这些错误取决于操作系统,因此没有标准的方法来报告发生了哪一个错误。但是,您可能能够通过对 this question 的回答获得错误代码。 .

如果您使用的是 Windows,请小心;如果出于与权限无关的各种原因报告错误代码 5(访问被拒绝)。

关于c++ - 文件流打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25408200/

相关文章:

c++ - 访问 vector vector 元素时出现段错误

c++ - C++ 中带有 std::for_each 的仿函数

c++ - 返回与作为参数传递的 lambda 表达式相同的类型

c++ - Ofstream 在 Linux 上写入空文件

c++ - 我无法让非常基本的命令提示 rune 本处理器正常工作。 ofstream() 的问题

c++ - 删除元素后列表的最后一个元素损坏

c++ - 如何使用 qtcreator 制作独立的 mac 应用程序

c++ - 在运行时链接 C++ .o 文件

c++ - 检查字符串是否在以字符串 vector 为值的映射的值中

c++ - C++ 中编译时生成的 block