Handling files in C ++

// ********************************************** *********
// archiv04.cpp
// Demonstrate success in file I / O operations
// © 1999, Jaime Virgilio Gómez Negrete
// ********************************************** *********

#include <fstream.h>
#include <stdlib.h>

int main ()
{
ifstream origin (“Tests.txt”);
char line [128];

if (source.fail ())
close << “Error when opening the file Tests.txt” << endl;
else
{
ofstream destination (“Copia.txt”, ios :: noreplace);
if (dest.fail ())
cerr << “Error creating the file: Copia.txt” << endl;
else
{
while (! origin.eof ())
{
origin.getline (line, sizeof (line));
if (origin.good ()) // if reading ok and
if (origin.eof ()) // if eof, -> ends
exit (1); // the program
else
destination << line << endl;
if (dest.fail ())
{
close << “Failure to write to file” << endl;
exit (1);
}
}
}
destination.close ();
}
origin.close ();

return 0;
}

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!