The use of streams with files. This programme copy the input file to another file and also print its contents to the IO Stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include <iostream.h> #include <fstream.h> #include <process.h> void main() { ifstream infile; ofstream outfile; ofstream printer; char filename[20]; cout << "Enter the desired file to copy ----> "; cin >> filename; infile.open(filename, ios::nocreate); if (!infile) { cout << "Input file cannot be opened.\n"; exit(1); } outfile.open("copy"); if (!outfile) { cout << "Output file cannot be opened.\n"; exit(1); } printer.open("PRN"); if (!printer) { cout << "There is a problem with the printer.\n"; exit(1); } cout << "All three files have been opened.\n"; char one_char; printer << "This is the beginning of the printed copy.\n\n"; while (infile.get(one_char)) { outfile.put(one_char); printer.put(one_char); } printer << "\n\nThis is the end of the printed copy.\n"; infile.close(); outfile.close(); printer.close(); } // Result of execution // // (The input file is copied to the file named "COPY") // (The input file is printed on the printer |