View Full Version: Reading/Writing to Files

C++ Learning Community > C++ Tips > Reading/Writing to Files


Title: Reading/Writing to Files


Dragon - June 24, 2003 11:35 PM (GMT)
This code reads text from one file and writes it to another. Create a file called "File1.txt" (or another name) and enter as many lines of text as you want. This will be the file that's going to be read. The text is going to be written to "File2.txt." (you can modify that as well).

QUOTE
#include <fstream>

using namespace std;

int main()
{
  //Open "File1.txt" for reading
  ifstream in("File1.txt");
  //Open "File2.txt" for writing
  ofstream out("File2.txt");
  string s;
  while (getline(in,s))
  {
    out << s << "\n";
  }
}




* Hosted for free by InvisionFree