-
copying binary file
Hi everybody,
I am new in C I have learned it just couple of months ago, I wrote program that reads a file and it must copy it to another file in binary mode, also I must use fseek(),ftel() and for loop in this assignment but I don’t know why the program is not working as expected. I appreciate if any body can help.
Code:
#include <stdio.h>
#define BUFLEN 1024
int main()
{
FILE *in;
FILE *out;
char buf[BUFLEN];
char in_name[12];
char out_name[12];
long file_size;
int i,num1,size_moved,length;
int *data;
printf("\nEnter the source file name: ");
scanf("%s",&in_name);
printf("\nEnter the destination file name: ");
scanf("%s",&out_name);
in = fopen(in_name,"rb"); /* open source file */
if (in == NULL)
{
puts("\nUnable to open the source file.");
return(0);
}
out = fopen(out_name,"wb"); /* open destination file */
if (out == NULL)
{
puts("\nUnable to create the destination file.");
return(0);
}
fseek(in, -1L, SEEK_END); /* move to the end of source file */
file_size = ftell(in); /* get file size */
fseek(in, 0L, SEEK_SET); /* move to the top of source file */
if (file_size <= BUFLEN) /* if source file size is 1024 byte */
{ /* long or less write to destination file */
length = file_size; /* in one time. */
fread(&data, length, 1, in);
fwrite(&data, length, 1, out);
}
else /* if source file size greater than 1024 byte */
{ /* write destination file many times using */
num1 = file_size / BUFLEN; /* for loop */
for (i=1;i<=num1;i++)
{
fread(&data, BUFLEN, 1, in);
fwrite(&data, BUFLEN, 1, out);
}
size_moved = BUFLEN * num1; /* calculate remaining of the file */
/* and write it to destination file */
length = file_size - size_moved;
fread(&data, length, 1, in);
fwrite(&data, length, 1, out);
}
/* Close the files */
fclose(in);
fclose(out);
return(0);
}
-
A few pointers:
>>scanf("%s",&in_name);
You don't need the &. Make it:
>>scanf("%s",in_name);
or better still, use fgets(), like in here:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
>>scanf("%s",&out_name);
Same thing
>>fread(&data, length, 1, in);
The data variable is a pointer, so you don't need the &. But then data isn't pointing to valid memory, so your use is way wrong. Maybe you meant to use the buf variable instead?
Why are you using fseek()/ftell() etc? It's unnecessary. Just read and write until you hit end of file. Something like:
Code:
while ((nbytes = fread(buf, 1, sizeof(buf), in)) > 0)
{
fwrite (buf, 1, nbytes, out);
}
-
Thank you Hammer for your reply, but is it possible to use For loop instead of While loop.
-
it would be kindof pointless but it could be written as
Code:
for( ;(nbytes=fread(buf,1,sizeof(buf),in)>0; ){
fwrite (buf,1,nbytes,out);
}
for loops are usually for programs that are going to loop either a constant number of times or a variable number of times. Use the while loop for function returns usually:)
-
It is always better to print the error messages to error output as in the code below,
Code:
if( in == NULL)
{
fprintf(stderr,"\nUnable to open the source file.");
return 1;
}
Whenever a program terminates abnormally due to some error, its exit code has to be non - zero (it is just a convention) i.e., 1 in the above code. So for each error quit in the program, different exit codes can be used. Exit code of 0 indicates successful execution of the program.
-
Thanks for all comments the program is working now.