C++ 使用 ifstream 检查文件是否成功打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6255339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a file opened successfully with ifstream
提问by ant2009
I have the following that will open a file for reading. However, I want to check to make sure that the file was open successfully, so I am using the fail to see if the flags have been set. However, I keep getting the following error:
我有以下内容将打开一个文件进行阅读。但是,我想检查以确保文件已成功打开,因此我使用失败来查看标志是否已设置。但是,我不断收到以下错误:
I am new to C++, as I am coming from C. So not sure I understand this error:
我是 C++ 新手,因为我来自 C。所以不确定我是否理解这个错误:
cannot call member function ‘bool std::basic_ios<_CharT, _Traits>::fail() const [with _CharT = char, _Traits = std::char_traits]' without object
不能在没有对象的情况下调用成员函数 'bool std::basic_ios<_CharT, _Traits>::fail() const [with _CharT = char, _Traits = std::char_traits]'
Code:
代码:
int devices::open_file(std::string _file_name)
{
ifstream input_stream;
input_stream.open(_file_name.c_str(), ios::in);
if(ios::fail() == true) {
return -1;
}
file_name = _file_name;
return 0;
}
采纳答案by Sven
You can simply do this:
你可以简单地这样做:
int devices::open_file(std::string _file_name)
{
ifstream input_stream;
input_stream.open(_file_name.c_str(), ios::in);
if(!input_stream)
{
return -1;
}
file_name = _file_name;
return 0;
}
fail() is not a static method, you must call it on an instance not a type, so if you want to use fail(), replace !input_stream
with input_stream.fail()
in my code above.
fail() 不是静态方法,你必须在一个实例上调用它而不是一个类型,所以如果你想使用 fail(),在我上面的代码中替换!input_stream
为input_stream.fail()
。
I do have to wonder what you're trying to achieve here. You're opening the file and immediately close it again. Are you simply trying to check if the file exists?
我确实想知道你想在这里实现什么。您正在打开文件并立即再次关闭它。你只是想检查文件是否存在?
回答by Duy ??ng
you can also use std::ifstream::is_open
. Returns true if a file is open and associated with this stream object.
你也可以使用std::ifstream::is_open
. 如果文件已打开并与此流对象关联,则返回 true。
// ifstream::is_open
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream ifs ("test.txt");
if (ifs.is_open()) {
// print file:
char c = ifs.get();
while (ifs.good()) {
std::cout << c;
c = ifs.get();
}
}
else {
// show message:
std::cout << "Error opening file";
}
return 0;
}
http://www.cplusplus.com/reference/fstream/ifstream/is_open/
http://www.cplusplus.com/reference/fstream/ifstream/is_open/
回答by user7116
Your error is because you are using ios::fail()
as a static method when it is actually a member method.
您的错误是因为您在ios::fail()
实际上是成员方法时将其用作静态方法。
if (input_stream.fail())
{
...
}
回答by user7116
You have to call fail() on the stream object. A more idiomatic way of doing this is:
您必须在流对象上调用 fail()。一种更惯用的方法是:
input_stream.open(_file_name.c_str(), ios::in);
if( ! input_stream ) {
return -1;
}