I try to read from a file and to write to another file an arbitrary number of bytes
Code:
int main (int argc, char* argv[])
{
FILE * pFileR;
FILE * pFileW;
char buffer[32];
pFileR = fopen (argv[1], "rb");
strcpy (writeFileName, "new_");
strcat (writeFileName, argv[1]);
pFileW = fopen (writeFileName, "w");
if (pFileR==NULL || pFileW==NULL)
{
if(pFileW!=NULL) fclose(pFileW);
if(pFileR!=NULL) fclose(pFileR);
fputs ("File error", stderr);
exit (1);
}
for(i=0; i<20; i++)
{
fread (buffer, 1, sizeof(buffer), pFileR);
fwrite (buffer, 1, sizeof(buffer), pFileW);
}
fclose(pFileR);
fclose(pFileW);
return 0;
}
The first file is bigger than the number of bytes I read from it. Then I use a file compare utility to compare the files. The problem is that only the first 338 bytes are ok. After that I get an extra space(ASCII) offset and of course the rest of the file is diferent. No matter the size of buffer, I always get only the first 338 bytes correctly written.