C/C++中文件输入输出流的一个示例(使用list容器)

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.欢迎关注,与我交流



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值