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");
}
//待续。。。