Why wont this copy my binary files?

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
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:

Crusty

Lifer
Sep 30, 2001
12,684
2
81
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.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,729
4,702
75
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.
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
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
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> 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)
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Also at the end, make sure to flush the output buffer.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,729
4,702
75
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.