c++简单的文本文件读写实例

来源:互联网 发布:王朔 知乎 编辑:程序博客网 时间:2024/06/05 07:01

#include <fstream.h>
void main(){
 ifstream fin("d://try.txt",ios::nocreate);//打开文件不存在返回error
 ofstream fout("d://out.txt",ios::app);//打开文件不存在创建,存在追加
    if(!fin){
  cout<<"File open error!/n";
  return ;
 }
 if(!fout){
  cout<<"File create error!/n";
  return ;
 }
 char c[80];
 while(!fin.eof()){
  fin.read(c,80); //从输入流中读出字符存放到c数组中
     cout.write(c,fin.gcount());
  fout.write(c,fin.gcount());//将数组中存放的实际字符个数写入到输出流中

 }
 fin.close();
 fout.close();
}

原创粉丝点击