C/C++中的文件输入输出流,可以从磁盘上的txt文件读取数字,该txt文本中一行有一个数字,有多行数字,下面的示例作用是:从txt文件中读取数字,并在控制台上输出,然后将数字以相同的格式写入到另一txt文件中.
<span style="white-space:pre"> </span>//取出txt文本中的数据
<span style="white-space:pre"> </span>ifstream ifs("N:\\MyProjects\\VC6.0\\inputData\\1p1p1input.txt");
list<int> nums;
while(!ifs.eof())
{
char strNum[32]={0};
ifs>>strNum;
if(strcmp(strNum," ")!=0)
{
nums.push_back(atoi(strNum));
}
}
ifs.close();
//取出list中的数据,并在控制台中输出
list<int>::const_iterator it;
for(it=nums.begin();it!=nums.end();it++)
{
cout<<*it<<endl;
}
//输出数据到新的txt文本中
ofstream outfile("N:\\MyProjects\\VC6.0\\outputData\\1p1p1output.txt",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(it=nums.begin();it!=nums.end();it++)
{
outfile<<*it<<endl;
}
outfile.close();
本人微信公众号:Yongf.欢迎关注,与我交流