ios::nocreate error while compiling a C++ code

2019-02-23 18:15发布

While, compiling a package, written in C++ on RHEL 5.0. I am getting the following error.

> error: nocreate is not a member of std::ios

The source-code corresponds to:

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


I have tried

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

as well as:

#g++ -O -o <file>

Please suggest a solution.

3条回答
霸刀☆藐视天下
2楼-- · 2019-02-23 18:34

Opening a file in read mode (ios::in) won't create it if it doesn't exist. You can just leave off the non-standard nocreate. And since in is the default for ifstream:

ifstream tempStr (argv[4]);
查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-23 18:48

You can open the file as a filehandle using fopen and O_CREAT|O_EXCL and then convert it to a stream using

__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!

查看更多
可以哭但决不认输i
4楼-- · 2019-02-23 18:49

ios::nocreate is not part of standard C++ - what are you expecting it to do?

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:

ifstream f( filename );
if ( ! f.is_open() ) {
    // open failed - probably because infput file does not exist  
}
查看更多
登录 后发表回答