I'm trying to read data from a file into a buffer and write it into another file (essentially copying a file). But when i change BUFFSIZE
to some bigger number, it leaves some content unread near the end of the file, meaning it does not copy the last couple of words into outfile
#define BUFFSIZE 50
int main(void)
{
void *buffer = (void *)(malloc(BUFFSIZE));
FILE *file = (fopen("file.txt", "r"));
FILE *outfile = (fopen("out.txt", "w"));
while (fread(buffer, BUFFSIZE, 1, file))
fwrite(buffer, BUFFSIZE, 1, outfile);
free(buffer);
fclose(outfile);
fclose(file);
}
It works fine when i set BUFFSIZE
to 1. My file isn't that big, around 20 lines of text. Why is this happening?