简体   繁体   English

文件流无法打开

[英]Filestreams won't open

I have the following C++ code as part of a larger program: 我将以下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())
...

It's supposed to open several output file streams which will then - if they are all open - do a series of operations which writes to each one. 应该打开几个输出文件流,然后,如果它们都已打开,则将执行一系列写入每个文件的操作。 However the program terminates early because the streams are never open: 但是,由于流永远不会打开,因此程序会提前终止:

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

The variables outputName are std::strings, which I thought was allowed in C++11 (I have the -std flag enabled for C++11 in my OpenBlocks compiler options). 变量outputName是std :: strings,我认为在C ++ 11中是允许的(我在OpenBlocks编译器选项中为C ++ 11启用了-std标志)。

I'm not sure why the streams won't open. 我不确定为什么流不会打开。

Thanks. 谢谢。

File streams could fail to open for a number of reasons, such as if the path is invalid, if access is denied, or if the file is already open for writing in another program. 文件流可能由于多种原因而无法打开,例如,路径无效,访问被拒绝或文件已经打开以便在另一个程序中写入。

These errors are OS-dependent, and thus there is no standard way of reporting which one occured. 这些错误与操作系统有关,因此没有报告发生错误的标准方法。 However, you may be able to obtain an error code with the answers to this question . 但是,您可能能够获得带有此问题答案的错误代码。

If you're using Windows, be careful; 如果您使用Windows,请当心; if reports error code 5 (access denied) for all sorts of reasons that have nothing to do with permissions. 如果由于种种与权限无关的原因而报告错误代码5(访问被拒绝)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM