next up previous contents index
Nächste Seite: 9.2 Dateneingabe und -ausgabe Aufwärts: 9. File Input und Vorherige Seite: 9. File Input und   Inhalt   Index


9.1 Kopieren von Files

Das folgende Programm kopiert ein Inputfile auf ein Outputfile, allerdings ohne Leerzeichen, Tabulatoren, Zeilenumbrüche. (siehe Ex911.cc)

//                     Ex911.cc
#include <iostream.h>
#include <fstream.h>

int main()
{
 char infilename[100], outfilename[100];
 char str[100];
 
 cout << " Input file: "; cin >> infilename;
 cout << "Output file: "; cin >> outfilename;

 ifstream  infile(infilename);
 ofstream outfile(outfilename); 
 
 while (infile.good())
 {
   infile >> str;
  outfile << str;
 }

 return 0;
}

Will man dagegen das File identisch kopieren, so muß auch zeichenweise ein- und ausgelesen werden. Hierzu werden die Methoden get und put aus den entsprechenden Streamklassen verwendet. (siehe Ex912.cc)

//                         Ex912.cc
#include <iostream.h>
#include <fstream.h>

int main()
{
 char infilename[100], outfilename[100];
 char ch;
 
 cout << " Input file: "; cin >> infilename;
 cout << "Output file: "; cin >> outfilename;

 ifstream  infile(infilename);
 ofstream outfile(outfilename); 
 
 while (infile.good())
 {
   infile.get(ch);
  outfile.put(ch);
 }
 return 0;
}


Gundolf Haase 2004-01-15