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); }



LinkBack URL
About LinkBacks


