+13  A: 

Use getline() to read the first line, then begin reading the rest of the stream.

ifstream stream("filename.txt");
string dummyLine;
getline(stream, dummyLine);
// Begin reading your stream here
while (stream)
   ...

(Changed to std::getline (thanks dalle.myopenid.com))

Doug T.
Using the free std::getline function would be a bit cleaner.
dalle
could use stream.ignore(). See below.
Martin York
+1  A: 

Call getline() once to throw away the first line

There are other methods, but the problem is this, you don't know how long the first line will be do you? So you can't skip it till you know where that first '\n' is. If however you did know how long the first line was going to be, you could simply seek past it, then begin reading, this would be faster.

So to do it the first way would look something like:

#include <fstream>
#include <iostream>
using namespace std;

int main () 
{
    // Open your file
    ifstream someStream( "textFile.txt" );

    // Set up a place to store our data read from the file
    string line;

    // Read and throw away the first line simply by doing
    // nothing with it and reading again
    getline( someStream, line );

    // Now begin your useful code
    while( !someStream.eof() ) {
        // This will just over write the first line read
        getline( someStream, line );
        cout << line << endl;
    }

    return 0;
}
Adam
A: 
#include <fstream>
#include <iostream>
using namespace std;

int main () 
{
  char buffer[256];
  ifstream myfile ("test.txt");

  // first line
  myfile.getline (buffer,100);

  // the rest
  while (! myfile.eof() )
  {
    myfile.getline (buffer,100);
    cout << buffer << endl;
  }
  return 0;
}
m_pGladiator
while (! myfile.eof() ): Fails after you try and read past the end of the line. So last line can be printed twice.
Martin York
+10  A: 

You could use the ignore feature of the stream:

ifstream stream("filename.txt");

// Get and drop a line
stream.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

// Get and store a line for processing.
// std::getline() has a third parameter the defaults to '\n' as the line
// delimiter.
std::string line;
std::getline(stream,line);

std::string word;
stream >> word; // Reads one space separated word from the stream.

A common mistake for reading a file:

while( someStream.good() )  // !someStream.eof()
{
    getline( someStream, line );
    cout << line << endl;
}

This fails because: When reading the last line it does not read the EOF marker. So the stream is still good, but there is no more data left in the stream to read. So the loop is re-entered. std::getline() then attempts to read another line from someStream and fails, but still write a line to std::cout.

Simple solution:
while( someStream ) // Same as someStream.good()
{
    getline( someStream, line );
    if (someStream) // streams when used in a boolean context are converted to a type that is usable in that context. If the stream is in a good state the object returned can be used as true
    {
        // Only write to cout if the getline did not fail.
        cout << line << endl;
    }
}
Correct Solution:
while(getline( someStream, line ))
{
    // Loop only entered if reading a line from somestream is OK.
    // Note: getline() returns a stream reference. This is automatically cast
    // to boolean for the test. streams have a cast to bool operator that checks
    // good()
    cout << line << endl;
}
Martin York
technically streams cast to void*, not bool but the idea is the same. Also it calls fail(), not good(), but I think it should work for most eof cases too.
Greg Rogers