• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Why wont this copy my binary files?

Onceler

Golden Member
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;

   printf("Enter name of file to copy\n");
   gets(source_file);

   source = fopen(source_file, "rb");

   if( source == NULL )
   {
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   printf("Enter name of target file\n");
   gets(target_file);

   target = fopen(target_file, "wb");

   if( target == NULL )
   {
      fclose(source);
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, target);

   printf("File copied successfully.\n");

   fclose(source);
   fclose(target);

   return 0;
}
it only works on txt files it write something to disk but it is only 1KB in size
 
Last edited:
Well fgetc will also return EOF when there's been an error reading the file, not just when it gets to the end. After you detect an EOF you need to call ferror(FILE *stream); to see if the error flag has been set or to see if you've actually reached the end of file.
 
gets is unsecure. Use fgets instead.

Also, for binary files, you should use (f)read and (f)write. Text reading/writing functions may convert between \n and \r\n, especially on Windows.
 
Code:
   while( ( ch = fread(source) ) != EOF )
      fwrite(ch, target);

   printf("File copied successfully.\n");

   fclose(source);
   fclose(target);

   return 0;
}
Code:
C:\Users\Benjamin\Documents\fcopy3.c||In function 'main':|
C:\Users\Benjamin\Documents\fcopy3.c|33|error: too few arguments to function 'fread'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|412|note: declared here|
C:\Users\Benjamin\Documents\fcopy3.c|34|warning: passing argument 1 of 'fwrite' makes pointer from integer without a cast [enabled by default]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|413|note: expected 'const void *' but argument is of type 'char'|
C:\Users\Benjamin\Documents\fcopy3.c|34|warning: passing argument 2 of 'fwrite' makes integer from pointer without a cast [enabled by default]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdio.h|413|note: expected 'size_t' but argument is of type 'struct FILE *'|
C:\Users\Benjamin\Documents\fcopy3.c|34|error: too few arguments to function 'fwrite'|
||=== Build finished: 2 errors, 2 warnings (0 minutes, 0 seconds) ===|
thats what happens when I try to put fread and fwrite in
 
> error: too few arguments to function 'fread'

Did you look up the parameters needed? "too few arguments" = not enough things passed to the function.

For example:
size_t fread (void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
size_t fwrite (const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
 
fclose should flush the output buffer.
`man fclose` said:
The fclose() function flushes the stream pointed to by fp (writing any
buffered output data using fflush(3)) and closes the underlying file
descriptor.
 
Back
Top