Due to some brain-damaged program requirements, the following function must copy from a FILE to an ostream. Why does it fail to work?
1 /************************************************
2 * copy -- Copy the input file to the output *
3 * file. *
4 ************************************************/
5 #include <cstdio>
6 #include <iostream>
7 #include <fstream>
8
9 /************************************************
10 * copy_it -- Copy the data *
12 void copy_it(
13 FILE *in_file, // Input file
14 std::ostream &out_file // Output file
15 )
16 {
17 int ch; // Current char
18
19 while (1) {
20 ch = std::fgetc(in_file);
21 if (ch == EOF)
22 break;
23 out_file << ch;
24 }
25 }
26
27 int main()
28 {
29 // The input file
30 FILE *in_file = std::fopen("in.txt", "r");
31 // The output file
32 std::ofstream out_file("out.txt");
33
34 // Check for errors
35 if (in_file == NULL) {
36 std::cerr <<
37 "Error: Could not open input\n";
38 exit (8);
39 }
40 if (out_file.bad()) {
41 std::cerr <<
42 "Error: Could not open output\n";
43 exit (8);
44 }
45 // Copy data
46 copy_it(in_file, out_file);
47
48 // Finish output file
49 std::fclose(in_file);
50 return (o);
51 }