iostream库支持文件的输入和输出。为了打开一个文件提供输入和输出,除了iostream头文件外,还需要包含:#include
下面是一个简单的程序。它从一个名为in_file的文本文件中读取单词,然后把每个单词写到一个名为out_file的输出文件中,并且每个词之间用空格分开。
代码#include<iostream>
#include<fstream>
#include<string>
usingnamespacestd;
intmain()
{
ofstream outfile("out_file.txt");
ifstream infile("in_file.txt");
if(!infile)
{cerr<<"error:unable to open input file!n";
return-1;}
if(!outfile)
{cerr<<"error:unalbe to open output file!n";
return-2;}
stringword;
while(infile>>word)
outfile<<word<<'';
return0;
}
如是,新建一个in_file.txt,内容是“This is my file”,则目录下出现out_file.txt
原文链接: https://www.cnblogs.com/ming5536/archive/2010/12/11/1903049.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/18644
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!