I made a simple C code for comparing two occasions of file stream. Both are reading .txt file and writing what it read to new .dat file(binary). The question is that the result file is different depending on the location of cursor in .txt file, which means whether '\n' exists at the end of file. Left case doesn't have '\n' at the last sentence, but right case does. I think this is related with "when does program notice 'EOF'?". Please help me.
and code is below.
#include <stdio.h>
#include <string.h>
int main(){
FILE* src = fopen("/Users/Chois/Desktop/test.txt", "rt");
FILE* dest = fopen("/Users/Chois/Desktop/dest.dat", "wb");
char buff[50];
int countOfWirte = 0;
if(src!=NULL && dest!=NULL){
fgets(buff, 50, src);
while(!feof(src)){
fwrite(buff, sizeof(char), 50, dest);
countOfWirte++;
fgets(buff, 50, src);
}
}else{
printf("fail");
}
printf("fwrite work %d times\n", countOfWirte);
fclose(src);
fclose(dest);
}
Thanks.