title: ifstream creates file if it doesn't exist
body: <p>I'm having some trouble writing a Linux console app which reads apache logs.</p>
  
  <p>I need to handle bash script arguments, the last one being a path to the log file.
  My problem is that if the file doesn't exist, I would like to throw an exception.</p>
  
  <p>But when I try to open the file in read-only mode, instead of failing it creates the file !</p>
  
  <p>Here's the code :</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// logreader.h
  
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;stdexcept&gt;
  
  
  class LogReader
  {
      public:
          LogReader(int, const char **);
          virtual ~LogReader();
  
          // ...
  
      private:
          std::ifstream log_;
  };
  
  // logreader.cpp
  
  #include &lt;logreader.h&gt;
  
  LogReader::LogReader(int argc, const char ** argv):
      log_()
  {
      log_.exceptions(std::ifstream::failbit | std::ifstream::badbit);
      for (int i = 1; i &lt; argc; ++i)
      {
          std::string arg(argv[i]);
          if (i == argc - 1)
          {
              try
              {
                  log_.open(arg.c_str(), std::ifstream::in);
              }
              catch (std::ifstream::failure)
              {
                  throw std::runtime_error("The file " + arg + " wasn't opened");
              }
          }
      }
  }
  
  LogReader::~LogReader()
  {
  }
  
  // main.cpp
  
  #include &lt;logreader.h&gt;
  
  int main(int argc, const char ** argv)
  {
      LogReader(argc, argv);
      return 0;
  }
  </code></pre>
  
  <p>Script call:</p>
  
  <pre><code>jmcomets $ ./test -g -l
  jmcomets $ ls -l
  -rw-rw-r-- 1 jmcomets jmcomets     0 Nov 14 22:41 -l 
  </code></pre>