isstringstream fstream文件读取操作

 1.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

void main()
{
 fstream file("E://msdn//a.txt");//不能放到中文路径下
 ofstream out("b.txt");
 string s,s1;


/* while(geline(file,s))
 {
  istringstream sin(s)// 操作文件方法一
  while(sin >>s1)
   cout << s1 << " ";
  cout << endl;
 }*/


 istringstream sin;
 while(getline(file,s))
 {
  sin.str(s);
  while(sin>>s1)//操作文件方法二
   cout << s1 << " " ;
  sin.clear();
  cout << endl;
 }
 system("pause");
}

 

2.

//*********************************************
//功能:中国通史这个文件的内容平均分为8分
//作者:XXX
//时间:2009.7.26
//*********************************************
#include<iostream>
#include <fstream>
#include <sstream>
#include<string>
using namespace std;

string  fun(int i)//创建文件
{
  char cNum[30];
  string fileName1;
   itoa(i,cNum,10);
   fileName1 += cNum;
   fileName1 += "_file.txt";

   return fileName1;
}

void main()
{
 const int iLength  = 500;
 ifstream fileIn("E://desk//vc//fileWriteOrRead//fengewenjian//fileDivise//fileDivise//TheDoneFile//zhongguotongshi.txt");
 
 int i = 0;
 int m = 0;
 int count = 0;
 int n = 1;
 string s;

 ofstream fileOut;
 fileOut.open(fun(i).c_str());
 while(getline(fileIn,s))
 {
   fileOut << s << endl;
      n++;
   if(n >iLength)
  {
   fileOut.close();
   i++;
   n = 1;
   fileOut.open(fun(i).c_str());
  }
 }
 fileOut.close();

 cout << "over!" << endl;
 system("pause");
}

 

//待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值