学习笔记:按行分拆txt文档

最近在做机器学习方面的东西。用到许多到txt的操作。其中有一个txt按行拆分的代码,留下来做笔记。

输入:combination_test.txt(一共有32行数据)
输出:32个单独的txt文档。名字分别为0.txt  1.txt   2.txt  ... ...


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

using namespace std;
string s[100];  
char data[2560];

void saveText(string fname,string str)
{
    ofstream fout(fname.c_str());
    fout<<str;
}


//读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
//If you want to avoid reading into character arrays, 
//you can use the C++ string getline() function to read lines into strings
void ReadDataFromFileLBLIntoString()
{
     ifstream fin("combination_test.txt");  
     int i=0;
     char a[25];
     string str0=".txt";
    while(getline(fin,s[i]) )
    {    
    itoa(i,a,10);
    string str=a+str0;
    saveText(str,s[i]);
    i++; 
    }
}

int main()
{
   ReadDataFromFileLBLIntoString(); //逐词读入字符串
   system("pause");
   return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值