Thread: copying binary file

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    10

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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    Thank you Hammer for your reply, but is it possible to use For loop instead of While loop.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    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

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    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.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    Thanks for all comments the program is working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM