title: Possible reasons for tellg() failing?
body: <p><code>ifstream::tellg()</code> is returning -13 for a certain file.</p>
  
  <p>Basically, I wrote a utility that analyzes some source code; I open all files alphabetically, I start with "Apple.cpp" and it works perfectly.. But when it gets to "Conversion.cpp", always on the same file, after reading one line successfully tellg() returns -13.</p>
  
  <p>The code in question is:</p>
  
  <pre><code>for (int i = 0; i &lt; files.size(); ++i) { /* For each .cpp and .h file */
     TextIFile f(files[i]);
     while (!f.AtEof()) // When it gets to conversion.cpp (not on the others)
                        // first is always successful, second always fails
        lines.push_back(f.ReadLine());
  </code></pre>
  
  <p>The code for <code>AtEof</code> is:</p>
  
  <pre><code>    bool AtEof() {
          if (mFile.tellg() &lt; 0)
              FATAL(format("DEBUG - tellg(): %d") % mFile.tellg());
          if (mFile.tellg() &gt;= GetSize())
              return true;
  
          return false;
      }
  </code></pre>
  
  <p>After it reads successfully the first line of Conversion.cpp, it always crashes with <code>DEBUG - tellg(): -13</code>.</p>
  
  <p>This is the whole <code>TextIFile</code> class (wrote by me, the error may be there):</p>
  
  <pre><code>class TextIFile
  {
  public:
      TextIFile(const string&amp; path) : mPath(path), mSize(0) {
          mFile.open(path.c_str(), std::ios::in);
  
          if (!mFile.is_open())
              FATAL(format("Cannot open %s: %s") % path.c_str() % strerror(errno));
      }
  
      string GetPath() const { return mPath; }
      size_t GetSize() { if (mSize) return mSize; const size_t current_position = mFile.tellg(); mFile.seekg(0, std::ios::end); mSize = mFile.tellg(); mFile.seekg(current_position); return mSize; }
  
      bool AtEof() {
          if (mFile.tellg() &lt; 0)
              FATAL(format("DEBUG - tellg(): %d") % mFile.tellg());
          if (mFile.tellg() &gt;= GetSize())
              return true;
  
          return false;
      }
  
      string ReadLine() {
          string ret;
          getline(mFile, ret);
          CheckErrors();
          return ret;
      }
  
      string ReadWhole() {
          string ret((std::istreambuf_iterator&lt;char&gt;(mFile)), std::istreambuf_iterator&lt;char&gt;());
          CheckErrors();
          return ret;
      }
  
  private:
      void CheckErrors() {
          if (!mFile.good())
              FATAL(format("An error has occured while performing an I/O operation on %s") % mPath);
      }
  
      const string mPath;
      ifstream mFile;
      size_t mSize;
  };
  </code></pre>
  
  <p>Platform is Visual Studio, 32 bit, Windows.</p>
  
  <p><em>Edit:</em> Works on Linux.</p>
  
  <p><em>Edit:</em> I found the cause: line endings. Both Conversion and Guid and others had \n instead of \r\n. I saved them with \r\n instead and it worked. Still, this is not supposed to happen is it?</p>