且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从文件读取和写入并将读取的数据存储到另一个文件中

更新时间:2023-02-07 11:24:52

//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;
}