如何阅读使用fstream的第二行的文本文件?(How do I read a text file

2019-09-02 05:24发布

我怎样才能让我std::fstream对象开始从第二行读取文本文件?

Answer 1:

使用函数getline()读取第一线,然后开始读取数据流的其余部分。

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

(改为标准::函数getline(感谢dalle.myopenid.com))



Answer 2:

您可以使用忽略流的特点:

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.

用于读取文件中的常见错误:

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

失败的原因是:当读取最后一行就不会读取EOF标记。 所以流还是不错的,但没有留在流中读取更多的数据。 因此,循环重新输入。 的std ::函数getline(),然后尝试读取从someStream和失败另一条线,但仍写一行到std ::法院。

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;
}


Answer 3:

更有效的方式是忽略用的std :: istream的字符串::忽视

for (int currLineNumber = 0; currLineNumber < startLineNumber; ++currLineNumber){
    if (addressesFile.ignore(numeric_limits<streamsize>::max(), addressesFile.widen('\n'))){ 
        //just skipping the line
    } else 
        return HandleReadingLineError(addressesFile, currLineNumber);
}

HandleReadingLineError不非标准,但手工制作的,当然。 第一个参数是字符提取的最大数量。 如果这正是numeric_limits ::最大(),没有任何限制:链接cplusplus.com: 性病:: istream的::忽视

如果你想跳过了很多行的你一定要使用它而不是函数getline的:当我需要跳过10万株在我的文件花了大约在相反的第二至22秒函数getline。



Answer 4:

调用函数getline()一次就扔掉的第一行

还有其他的方法,但问题是这样的,你不知道多久,第一行是你呢? 所以,直到你知道那首“\ n”是你不能跳过它。 然而,如果你是知道的第一行是要去多久是,你可以简单地寻找过去,然后开始阅读,这会更快。

因此,要做到这一点的第一种方法看起来是这样的:

#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;
}


Answer 5:

#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;
}


Answer 6:

#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;
}


文章来源: How do I read a text file from the second line using fstream?
标签: c++ fstream