| Hazer (38) | |
|
Hi everyone! I have a little problem. Code: #include <stdio.h> #include <iostream> using namespace std; int main() { FILE *in, *out; in=fopen("C:\\input.txt","rb"); out=fopen("C:\\output.txt","wb"); char c; while(c!=EOF) {c=fgetc(in); fputc(c, out); } fclose(in); fclose(out); return 0; } this program must copy files but only txt files are copied correctly. When i try to copy .exe it is not copied or copied incorrectly. Please tell what am i doing wrong. Thanks. | |
|
|
|
| mm148881 (7) | |
|
Hi, One should use the macro EOF only to check the end of text files. As far as I understand it - but I am new to C and C++ - a binary file, depending on the OS, might contain many characters equal to EOF not only at its very end. To detect the end of a binary file you can either use the function feof(in) of the cstdio library, or within the fstream class the member function eof(). Also, and somebody should correct me if I am wrong, for the "fstream in", in.eof() is true only after an attempt to read beyond the eof has been done. Thus, it is wrong to say: while(in.eof()){ c=in.get(); out.put(c)l } I rewrote you code with something I think sould also work on windows. #include <fstream> #include <iostream> using namespace std; int main (void ){ fstream in("input.bin",ios::binary|ios::in); fstream out("output.bin",ios::binary|ios::out); char c; while(true) { c=in.get(); if(in.eof()) break; out.put(c); } in.close(); out.close(); return 0; } Cheers Max | |
|
|
|
| Hazer (38) | |
| Thanks. I used feof() and EOF before but I thought they are epual. | |
|
|
|