When you reach the end of file, you don't break
the loop. So you are calling fputc(c, fpOut);
with c==EOF
which is probably an undefined behavior, or at least the writing of a xff
byte.
And you don't call fclose
inside your while(argc--)
loop, so your files (except the last) are mostly never closed nor flushed.
At last, you don't test the result of fopen
and you should test that it is non null (and print an error message, perhaps with something about strerror(errno)
or perror
, in that case).
You should have found out with a debugger (like gdb
on Linux), and perhaps with the help of compiler warnings (but gcc-4.6 -Wall
did not caught any bugs on your example).
You could decide that the output file name is related to input file name, perhaps with
char outname[512];
for(i = 1; i < argc; i++) {
fpIn = fopen(argv[i], "rb");
if (!fpIn) { perror (argv[i]); exit(1); };
memset (outname, 0, sizeof (outname));
snprintf (outname, sizeof(outname)-1, "%s~%d.out", argv[i], i);
fpOut= fopen(outname, "wb");
if (!fpOut) { perror (outname); exit(1); };
/// etc...
fclose(fpIn);
fclose(fpOut);
fpIn = fpOut = NULL;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…