如何从文件读取和写入并将读取的数据存储到另一个文件中 [英] How to Read and Write from a file and store the read data into another file

查看:107
本文介绍了如何从文件读取和写入并将读取的数据存储到另一个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何一次从文件中读取和写入并将读取的数据存储到另一个文件中。请任何人帮助我..

How to Read and Write from a file at once and store the read data into another file.Please anybody help me..

推荐答案

//Open a file
//you first operation generally performed on an object of one of these classes is to associate it to a real file
//text file streams are those where the ios::binary flag is not included in their opening mode. 
// and then writing on a text file
#include <iostream>
#include <fstream>
using namespace std;


int main () {
  string line;
  ifstream myfile ("read.txt");
  ofstream writefile;
  
  if (myfile.is_open())
  {
    writefile.open ("write.txt");
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
	  writefile << line << '\n';
    }
	writefile.close();
    myfile.close();
  }
  else cout << "Unable to open file"; 
  return 0;
}


这篇关于如何从文件读取和写入并将读取的数据存储到另一个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆