c++ txt文本文件提取

#include <string> #include <iostream>
#include
<fstream>
#include
<sstream>

using namespace std;

int main(int argc, char* argv[])
{
    ifstream ifile(
"1.txt");
   
string line, newline("");
   
int cnt=0//计数器
    getline(ifile,line);  //读取文件一行
    while (!ifile.fail())
    {
//循环读取文件
        cnt++//计数器累加
        newline+=line;  //字符串追加
        if (cnt==6//读取到6行时变更处理方式
        {
            newline
+='/r/n'//增加回车换行符号
            cout<<newline<<endl<<endl;  //测试程序是输出,可以改为写文件
newline=""//初始化辅助数据
            cnt=0;
        }
       
else
        {
            newline
+=','//未满6行时,追加,字符
        }
        getline(ifile,line); 
//继续读取一行
    }
   
   
return 0;
}




测试文本1.txt:
$WFE,1000.0440,W,09000.0000,Y,X,X,AA6
9980,0000043,0003440,040,040,040,002,023,030
8000,0054500,0005640,050,040,040,030,003,030
8596,0076700,0006340,300,450,023,230,300,030
5678,0004556,0045600,600,400,050,034,030,300
5665,4353000,0232434,340,034,045,320,030,200
$WFE,1000.0440,W,09000.0000,Y,X,X,AA6
9980,0000043,0003440,040,040,040,002,023,030
8000,0054500,0005640,050,040,040,030,003,030
8596,0076700,0006340,300,450,023,230,300,030
5678,0004556,0045600,600,400,050,034,030,300
5665,4353000,0232434,340,034,045,320,030,200

输出结果:
$WFE,1000.0440,W,09000.0000,Y,X,X,AA6,9980,0000043,0003440,040,040,040,002,023,0
30,8000,0054500,0005640,050,040,040,030,003,030,8596,0076700,0006340,300,450,023
,230,300,030,5678,0004556,0045600,600,400,050,034,030,300,5665,4353000,0232434,3
40,034,045,320,030,200

$WFE,1000.0440,W,09000.0000,Y,X,X,AA6,9980,0000043,0003440,040,040,040,002,023,0
30,8000,0054500,0005640,050,040,040,030,003,030,8596,0076700,0006340,300,450,023
,230,300,030,5678,0004556,0045600,600,400,050,034,030,300,5665,4353000,0232434,3
40,034,045,320,030,200

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值