I try to copy the file using this function, but we have strange characters in the output file.
Int filecopy (four filesource [], four file dependencies []) {good result = -1; Four c [1]; File * stream_r = fopen (file resource, "r"); FILE * stream_w = FOPN (file specificity, "W"); // When typing to write and write in file (([0] = (four) fgetc (stream_R)) = EOF {fprintf (stream_W, c); } // close clauses fclose (stream_R); Falcos (stream_w); Return result; }
I do not know what is wrong. help please.
The problem is that c [1]
will not work for a string In form, because it can not eliminate null
byte, it should be
char c [2] = {0};
and c [2]
should be int
, like
int c [2 ] = {0}; Because fgetc ()
returns int
so that your code is potentially overflowing c [0]
, But you also have some other things that you can improve.
-
You do not need c
for a code, you can declare it like this. / P>
int c;
and then fprintf ()
instead of fputc (); Use
.
-
You have to check that none of the fopen ()
calls are unsuccessful, otherwise your program \ code> NULL
indicator Due to Derences will introduce undefined behavior. This is a strong version of your own program, which is with the problem fixed in your question
/ * ** Function Return Value Mean * -1 Can not Open Source File * -2 Destination File Can not Open * 0 Success * / Int Filecopy (Four File Resources [], Four File Definitions []) {int c; File * stream_r; FILE * stream_W; Stream_r = FOPN (file processing, "r"); If (stream_r == faucet) return -1; Stream_w = FOPN (file density, "W"); // Write to write and write file (stream_W == NULL) {fclose (stream_R); Return -2; } While ((c = fgetc (stream_R)! = EOF) fputc (c, stream_W); Falcos (stream_r); Falcos (stream_w); Return 0; }
No comments:
Post a Comment