每 X 行读取并写入文件
#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 行读取一次并将其写入我的文件中。
我该怎么做呢?
#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;
}
I want to make this program read every 10th line and write it into my file.
How do i go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要读取每一行并增加一个计数器。如果计数器达到 10,则需要写入该行并重置计数器。
You need to read every line and increment a counter. If the counter reach 10, you need to write the line and reset the counter.