So I was trying to practice C as opposed to C++ which I'm more or less used to (at least when it comes to doing hw assignments), and I wrote the following code:
Now obviously my first problem was using feof() incorrectly, but before finding that out I noticed something weird. although I call free() AFTER I call fwrite(), commenting out the free() line changes the extra character in the file. If I have free(), the byte is 00. If I don't, though, the byte is the character for 'x' . Does anyone know why this would be? I would think that code executed after writing to the file would have no effect.Code:#include <stdio.h> #include <stdlib.h> int main() { FILE* inFile = fopen("in", "r"); FILE* outFile = fopen("out", "w"); while(!feof(inFile)) { char* blah = malloc(sizeof(char)); fread(blah, sizeof(char), 1, inFile); fwrite(blah, sizeof(char), 1, outFile); free(blah); } fclose(inFile); fclose(outFile); return 0; }



LinkBack URL
About LinkBacks


