32

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;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • 3
    I prefer using `fin` (*file input*, similar to how `cin` is *console input*) instead of `input_stream`, since it's easier to write and it is perfectly understandable what it does. – H-005 Sep 04 '20 at 17:03

4 Answers4

27

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?

Sven
  • 21,903
  • 4
  • 56
  • 63
  • Hello, thanks for the answer. HOwever, I am just wondering why the fail flag never worked for me? – ant2009 Jun 06 '11 at 16:53
  • 1
    Using `input_stream.fail()` should have the same effect as `!input_stream`. As I said, you can't use `ios::fail()` because it's an instance method, not a static method. – Sven Jun 06 '11 at 16:57
  • 3
    Please, why use `ios::in` flag with `ifstream`? – nn0p Mar 30 '15 at 16:39
7

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/

Duy Đặng
  • 409
  • 1
  • 7
  • 15
5

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

if (input_stream.fail())
{
    ...
}
user7116
  • 63,008
  • 17
  • 141
  • 172
3

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