如何让我的std::fstream
对象从第二行开始读取文本文件?
8 回答
您可以使用流的忽略功能:
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::cout。
简单的解决方案: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;
}
}
正确解决方案:
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;
}
使用 getline() 读取第一行,然后开始读取流的其余部分。
ifstream stream("filename.txt");
string dummyLine;
getline(stream, dummyLine);
// Begin reading your stream here
while (stream)
...
(更改为 std::getline(感谢 dalle.myopenid.com))
更有效的方法是使用std::istream::ignore忽略字符串
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::max(),则没有限制:cplusplus.com 上的链接:std::istream::ignore
如果你要跳过很多行,你肯定应该使用它而不是 getline:当我需要跳过文件中的 100000 行时,它需要大约一秒钟,而使用 getline 需要 22 秒。
调用 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;
}
#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;
}
您可以使用以下忽略功能:
fstream dataFile("file.txt");
dataFile.ignore(1, '\n'); // ignore one line
#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;
}