0

I found a c++ code which uses ifstream to decide whether a file exists. That section is,

if (!ifstream(trajectory_file_name_)) {
    cerr << "ERROR: The trajectory file:";
    cerr << "\033[1;34m";
    cerr << trajectory_file_name_;
    cerr << "\033[0m";
    cerr << " does not exist.";
    cerr << endl;
    exit(1);
}

what I have learned from the textbook (like "c++ primer plus") is something like, when judging whether a file exist via ifstream,

ifstream inFile(filename-);
if(!inFile.is_open())
{
  return;
}

I have searched many website but didn't find any information on the first one. So I wonder if anyone can give me some explanation on the usage of ifstream of the first one. Thanks!

FaDA
  • 103
  • 1
  • 3
  • I would not use the first one. Yes, it tells you if the file could be opened at that exact time, but there's no guarantee you can open it again later to actually do something with it. – Retired Ninja Jul 04 '19 at 14:07

1 Answers1

2

This comes down to how streams convert to bool (or in this case it's due to operator!, but same principle).

It's designed so that, for short, you can check for openness (and error flags) like:

std::ifstream ifs("path");
if (!ifs)
{
   // ...
}

(Failure to open sets the failbit).

Your version just skips the declaration and does the same thing with a temporary.

Note that there are other reasons a file may not be openable, e.g. permissions. This is not a reliable way to check whether it exists and, even if it does, you have no way of guaranteeing that it will still exist in a moment when you try to do something with it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Isn't `operator!` called instead of the `bool` conversion? [This one](https://en.cppreference.com/w/cpp/io/basic_ios/operator!).. not sure why both exist in the first place. – lubgr Jul 04 '19 at 14:04
  • Thanks for your reply! I understand the code you mentioned. It defines a new class of the ifstream type named ifs. But what I can't understand is what ifstream(trajectory_file_name_) means in the first code. Ifstream is a class as I know, is "trajectory_file_name_" being used as a parameter? So "ifstream(trajectory_file_name_)" means some kind of function? – FaDA Jul 04 '19 at 14:20
  • 1
    @FaDA no, it is creating an object of type `ifstream` with no name. That object is constructed with the value `trajectory_file_name_` – Caleth Jul 04 '19 at 14:21
  • @Caleth Thanks a lot! I start to understand. So this 'no name' object work the same as the $ifs$. Would you please give me some references on this usage? I have checked many textbooks on C++ without finding any information. This really puzzles me a lot – FaDA Jul 04 '19 at 14:34
  • @FaDA it's the same sort of thing as when you write `std::cout << 1 + 1;`. Neither of the 1s, nor the 2 have a name in that expression. – Caleth Jul 04 '19 at 14:42
  • @lubgr Well, granted. Close enough. – Lightness Races in Orbit Jul 04 '19 at 14:48
  • @FaDA Turn to the page in your C++ book about "temporaries" – Lightness Races in Orbit Jul 04 '19 at 14:50