title: Unable to open ifstream with console input name
body: <p>I have faced a strange situation. I have built a code to compare the content of multiple pairs of text files. The names of each pair of files are given from a text file called batchfile.txt in the form:
  filename1.txt,filename2.txt
  filename3.txt,filename4.txt
  The problem is that the files do not open despite the fact that they are in the same directory as batchfile.txt which opens... I tried also with filenames as strings (I compile with C++11) but the result is the same.
  The strangest thing is that it runs OK when debugging... Any suggestion would be appreciated. Thank you!
  Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string.h&gt;
  #include &lt;string&gt;
  #include &lt;limits&gt;
  
  
  using namespace std;
  
  void compare (ifstream &amp;file1, ifstream &amp;file2)   {
      string f1, f2;
      int i(0), mark(0);
      while(getline(file1, f1) &amp;&amp; getline(file2, f2)) {
          i++;
          if (f1!=f2) {
              cout &lt;&lt; "ERROR IN LINE " &lt;&lt; i &lt;&lt; endl;
              cout &lt;&lt; "FIRST FILE: " &lt;&lt; f1 &lt;&lt; endl;
              cout &lt;&lt; "SECOND FILE: " &lt;&lt; f2 &lt;&lt; endl;
              mark=-1;
          }
      }
      if(!mark)
          cout &lt;&lt; "THE FILES ARE IDENTICAL" &lt;&lt; endl &lt;&lt; endl;
  }
  int main(int argc, char *argv[])
  {
      ifstream file1, file2, filebatch;
      string f1, f2, fb;
      string namef1, namef2;
      string bat;
      int intbat;
      size_t count(0);
      cout &lt;&lt; "Want to compare more pairs of text files? y/n " &lt;&lt; flush;
      cin &gt;&gt; bat;
      cin.clear();
      cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
      cout &lt;&lt; endl;
      if(bat=="y" || bat=="Y") intbat=1;
      else if(bat=="n" || bat=="N") intbat=0;
      else {
          intbat=0;
          cout &lt;&lt; "Unknown answer. One pair is considered." &lt;&lt; endl;
      }
      if(!intbat) {
           if(argc==1) {
           cout &lt;&lt; "NAME OF THE FIRST FILE: " &lt;&lt; flush;
           cin &gt;&gt; namef1;
           cin.clear();
           cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
           cout &lt;&lt; "NAME OF THE SECOND FILE: " &lt;&lt; flush;
           cin &gt;&gt; namef2;
           cin.clear();
           cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
      }
      else{
          namef1=argv[1];
          namef2=argv[2];
      }
      file1.open(namef1.c_str(), ios::in);
      if (!file1)
      {
          // NO, abort program
          cerr &lt;&lt; "Unable to open file1. " &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
      file2.open(namef2.c_str(), ios::in);
      if (!file2)
      {
          // NO, abort program
          cerr &lt;&lt; "Unable to open file2. " &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
      cout &lt;&lt; endl;
      compare(file1, file2);
  }
  else{
      filebatch.open("batchfile.txt", ios::in);
      if (!filebatch)
      {
          // NO, abort program
          cerr &lt;&lt; "Unable to open batchfile.txt " &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
  }
  while(getline(filebatch, fb))  {
      count=fb.find(",", 0);
      namef1=fb.substr(0, count);
      namef2=fb.substr(count+1);
      file1.open(namef1.c_str(), ios::in); 
      file2.open(namef2.c_str(), ios::in);
      if (file1.is_open() &amp;&amp; file2.is_open()) {  // the problem seems to be here
          cout &lt;&lt; namef1 &lt;&lt; " " &lt;&lt; namef2 &lt;&lt; endl;
          compare(file1, file2);
          cout &lt;&lt; endl;
      }
      else    {
          // NO, abort program
          cerr &lt;&lt; "Unable to open one of the file. " &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
  }
  file1.close();
  file2.close();
  return 0;
  }
  </code></pre>