How do I read a text file from the second line using fstream?

后端 未结 8 1656
野趣味
野趣味 2020-12-31 03:19

How can I make my std::fstream object start reading a text file from the second line?

相关标签:
8条回答
  • 2020-12-31 03:55

    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))

    0 讨论(0)
  • 2020-12-31 03:56
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string textString;
    string anotherString;
    ifstream textFile;
    textFile.open("TextFile.txt");
    if (textFile.is_open()) {
        while (getline(textFile, textString)){
            anotherString = anotherString + textString;
        }
    }
    
    std::cout << anotherString;
    
    textFile.close();
    return 0;
    }
    
    0 讨论(0)
  • 2020-12-31 03:56

    You can use ignore function as follow:

    fstream dataFile("file.txt");
    dataFile.ignore(1, '\n'); // ignore one line
    
    0 讨论(0)
  • 2020-12-31 03:58

    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;
    }
    
    0 讨论(0)
  • 2020-12-31 04:02

    this code can read file from your specified line from file but you have to make file in file explorer before hand my file name is "temp" code is given below

    https://i.stack.imgur.com/OTrsj.png

    hope this can help

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题