#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream stream1("source.txt");
string line ;
ofstream stream2("target.txt");
while( std::getline( stream1, line ) )
{
stream2 << line << endl;
cout << line << endl;
}
stream1.close();
stream2.close(); return 0;
}
我想让这个程序每隔10行读一次,然后把它写进我的文件。
我该怎么做?
答案 0 :(得分:3)
您需要读取每一行并递增一个计数器。如果计数器达到10,则需要写入该行并重置计数器。
int lineNumber = 0;
while( std::getline( stream1, line ) )
{
if (lineNumber == 10)
{
stream2 << line << endl;
cout << line << endl;
lineNumber = 0
}
lineNumber++;
}