使用 ifstream 检查文件是否成功打开

发布于 2024-11-13 20:38:03 字数 555 浏览 1 评论 0原文

我有以下内容将打开一个文件进行阅读。但是,我想检查以确保文件已成功打开,因此我使用失败来查看标志是否已设置。但是,我不断收到以下错误:

我是 C++ 新手,因为我来自 C。所以不确定我是否理解此错误:

无法调用成员函数 'bool std::basic_ios<_CharT, _Traits>::fail() const [with _CharT = char, _Traits = std::char_traits]' without object

代码:

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;
}

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:

cannot call member function ‘bool std::basic_ios<_CharT,
_Traits>::fail() const [with _CharT = char, _Traits = std::char_traits]’ without object

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路黄昏 2024-11-20 20:38:03

您可以简单地这样做:

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() 不是静态方法,您必须在实例而不是类型上调用它,因此如果您想使用fail(),请将 !input_stream 替换为 我上面的代码中的 input_stream.fail()

我确实想知道你想在这里实现什么目标。您正在打开该文件并立即再次将其关闭。您只是想检查文件是否存在吗?

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.

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?

隔岸观火 2024-11-20 20:38:03

您还可以使用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/

you can also use std::ifstream::is_open. Returns true if a file is open and associated with this stream object.

// 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/

〃安静 2024-11-20 20:38:03

您的错误是因为您将 ios::fail() 用作静态方法,而它实际上是成员方法。

if (input_stream.fail())
{
    ...
}

Your error is because you are using ios::fail() as a static method when it is actually a member method.

if (input_stream.fail())
{
    ...
}
迷你仙 2024-11-20 20:38:03

您必须对流对象调用fail()。更惯用的方法是:

input_stream.open(_file_name.c_str(), ios::in);

if( ! input_stream ) {
    return -1;
}

You have to call fail() on the stream object. A more idiomatic way of doing this is:

input_stream.open(_file_name.c_str(), ios::in);

if( ! input_stream ) {
    return -1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文