简体   繁体   English

编译C ++代码时出现ios :: nocreate错误

[英]ios::nocreate error while compiling a C++ code

While, compiling a package, written in C++ on RHEL 5.0. 同时,编译一个在RHEL 5.0上用C ++编写的包。 I am getting the following error. 我收到以下错误。

> error: nocreate is not a member of std::ios >错误: nocreate不是std::ios的成员

The source-code corresponds to: 源代码对应于:

ifstream tempStr( argv[4] , ios::in | ios::nocreate ); ifstream tempStr( argv[4]ios::in | ios::nocreate );


I have tried 我努力了

#g++ -O -Wno-deprecated <file.cpp> -o <file> #g ++ -O -Wno-deprecated <file.cpp> -o <file>

as well as: 以及:

#g++ -O -o <file> #g ++ -O -o <file>

Please suggest a solution. 请提出解决方案。

ios::nocreate is not part of standard C++ - what are you expecting it to do? ios::nocreate不是标准C ++的一部分 - 你期望它做什么?

Edit: From a Google, it seems like it was intended to prevent the file being created if it doesn't already exist. 编辑:从谷歌看来,它似乎是为了防止文件被创建(如果它尚不存在)。 This is the default for ifstreams anyway, so you can just say: 这是ifstreams的默认值,所以你可以说:

ifstream f( filename );
if ( ! f.is_open() ) {
    // open failed - probably because infput file does not exist  
}

Opening a file in read mode ( ios::in ) won't create it if it doesn't exist. 如果文件不存在, ios::in读取模式( ios::in )打开文件将不会创建它。 You can just leave off the non-standard nocreate . 你可以放弃非标准的nocreate And since in is the default for ifstream : 因为inifstream的默认值:

ifstream tempStr (argv[4]);

You can open the file as a filehandle using fopen and O_CREAT|O_EXCL and then convert it to a stream using 您可以使用fopen和O_CREAT | O_EXCL将文件作为文件句柄打开,然后使用它将其转换为流

__gnu_cxx::stdio_filebuf<char> filebuf(posix_handle, std::ios::out);
    ostream os(&filebuf);

this uses a non standard extension defined in . 这使用了中定义的非标准扩展。 If anyone has a better solution, i really want to know! 如果有人有更好的解决方案,我真的很想知道!

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

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