title: Reading lines of text file in C++ fails due to hidden/control characters
body: <p>I have two text files, which appear to be identical in a text editor, but my C++ code for reading the files produces different line-counts for each file.  I can't figure out where the files are different, or how to accommodate such difference in my C++ code.</p>
  
  <p>Let me explain...</p>
  
  <p>I have two text files, d1.txt and d2.txt.  Each contains 100 numbers, 1 per line.  When I open either of the files in vim and enter <code>:set list!</code>, there are only 100 lines, each containing a number and the end-of-line character ($) after the last number on each line.  In other words, when looking at them in vim, they <em>look</em> identical, with the exception of different precision in the numbers.  There is different precision because one file came from MATLAB and the other from Gnumeric.</p>
  
  <p>A quick diff of the files renders the following output (I use braced elipses "[...]" to omit portions in the interest of space):</p>
  
  <pre><code>1,28c1,28
  &lt; 0.01218465532007
         [...]
  &lt; 0.01327976337895
  ---
  &gt; 0.0121846553200678
         [...]
  &gt; 0.0132797633789485
  30,100c30,100
  &lt; 0.01329705254301
         [...]
  &lt; 0.00017832496354
  ---
  &gt; 0.0132970525430057
         [...]
  &gt; 0.000178324963543758
  \ No newline at end of file
  </code></pre>
  
  <p>Despite the message about the absence of a newline at the end of the second file (d2.txt), I can't <em>see</em> any difference when examining the last lines of the files in vim, as I mentioned above.</p>
  
  <p>I have created a C++ function <code>readVectorFromFile(std::vector&lt;double&gt;&amp;,const string)</code> that returns the number of lines read from the respective text file.  When I read the text files using the code:</p>
  
  <pre><code>std::cout &lt;&lt; "d1.txt has " &lt;&lt; readVectorFromFile(v1,"./d1.txt") &lt;&lt; " lines.\n";
  std::cout &lt;&lt; "d2.txt has " &lt;&lt; readVectorFromFile(v1,"./d1.txt") &lt;&lt; " lines.\n";
  </code></pre>
  
  <p>I get the output:</p>
  
  <pre><code>d1.txt has 99 lines.
  d2.txt has 100 lines.
  </code></pre>
  
  <p>The function is defined in the following way:</p>
  
  <pre><code>int readVectorFromFile(vector&lt;double&gt;&amp; vec, const string&amp; fullFilePathName) {
  
      int value, numLines;
      char line[10000];
      ifstream inFile;
  
      /* attempt to open file */
      inFile.open(fullFilePathName.c_str());
      if (inFile.fail()) {
          LOG(FATAL) &lt;&lt; "Unable to open file \"" &lt;&lt; fullFilePathName.c_str() &lt;&lt; "\" for reading.";
      } else {
          cout &lt;&lt; "Importing vector from file " &lt;&lt; fullFilePathName.c_str() &lt;&lt; "\n";
      }
  
      /* records the number of lines in the input file */
      numLines = static_cast&lt;int&gt;( count(istreambuf_iterator&lt;char&gt;(inFile),
                                         istreambuf_iterator&lt;char&gt;(), '\n') );
  
      /* start file over from beginning */
      inFile.clear();
      inFile.seekg(0, ios::beg);
  
      vec.clear(); // clear current vec contents
      vec.reserve(numLines);
  
      /* read value from each line of file into vector */
      for(int i=0; i&lt;numLines; ++i) {
          inFile.getline(line, 10000);
          vec.push_back( strtod(line,NULL) );
      }
  
      inFile.close(); // close filestream
  
      return numLines; // return the number of lines (values) read
  
  }
  </code></pre>
  
  <p>Why can I not see the difference between these files when I view them in vim?  Is there anything fundamentally wrong with the above function that is causing this problem?</p>