SmileeTiger
Programmer
Can someone give me some sample code to read from and write to a file in text mode using fstream?
Thanks
Thanks
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <fstream>
ofstream ArchiveFile; // opens stream named ArchiveFile for output
ArchiveFile.open("OutPut.dat", ios::out | ios::app); // opens for append so original isn't overwritten (out is redundant)
if (!ArchiveFile)
{
// Stop processing! There is an error.
Application->MessageBox("Cannot create archive file.", "File Error", MB_OK); // error message box
return; // get out of function
}
ifstream FNFile; // stream set up for input
FNFile.open("File2Get.dat");
if (!FNFile)
{
// Error opening file! Stop processing.
Application->MessageBox("Cannot open file to archive.", "File Error", MB_OK);
ArchiveFile.close(); Close open output file
return; // Get out of function
}
else
{
// No errors so let's process
string ArcLine; // this will be the line to copy from one file to another
while (getline(FNFile, ArcLine, '\n'))
{
// Put line to output
ArchiveFile << ArcLine << endl;
// You could also do a character by character copy, too
}
}
// All done so close files
ArchiveFile.close();
FNFile.close();
std::