#include <iostream>#include<fstream>#include<string>intMain () {using namespacestd; stringfilename; cout<<"Enter Name of the new file:"<<Endl; CIN>>filename; //Type 1//ofstream Fout (Filename.c_str ()); //Type 2Ofstream Fout; Fout.open (Filename.c_str ()); //If a file name is declared as "string", then you must use "C_str", however, when you declare that a file name is a character array type, there is no need to use/Fout<<"This is my test of file read and write \ n" ; Fout.close (); //**********read*****************//ifstream Fin (FILENAME.C_STR ()); Charch; //Type 1//While (Fin.get (CH))//read one character at a time to CH//cout << Ch <<endl; //Type 2Fin >> ch;//reads a character T stringstr; Fin>> str;//Read thiscout << str <<Endl; //Type 3//Char ch2[100]; //Fin.getline (ch2,100); //int i = 0; //While (i<40)//{ //cout << "ch2[" << I << "]:" <<ch2[i]<< Endl; //i++; //} //Type 4//string buff; //getline (Fin,buff); //cout<<buff<< "\ n"; //cout << "done\n"; return 0;}
C + + read and write files