Thread: Need help with file_copy program

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    Need help with file_copy program

    Hi,
    I wrote this simple file copy program to copy contents from input to output file. The program kind of rins into an infinite loop at read/write stage. Can anyone please point out where I am going wrong.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdlib.h>
    
    /* size of chunk to be transfered */
    #define CHUNK_SIZE 512
    
    int main(int argc, char *argv[]){
    
            int infile_fd, outfile_fd;                      /* input and output file descriptors */
            int error_read = 0;      /* variables to record error status */
            long *ptr;
    
            /* check for right cmd line arguments */
            if(argc != 3){
                    fprintf(stdout,"Usage : %s <input_file> <output_file>\n", argv[0]);
                    exit(1);
            }
    
            /* Open input file for reading */
            if((infile_fd  = open(argv[1], O_RDONLY)) == -1){
                    fprintf(stdout, "Cannot open file for reading\n");
                    exit(1);
            }
    
            /* open output file for writing */
            if((outfile_fd = open(argv[2], O_WRONLY | O_CREAT)) == -1){
                    fprintf(stdout, "Cannot open file for writing\n");
                    exit(1);
            }
    
            /* Alllocate CHUNK SIZE of array from heap */
            ptr = (void *)malloc(CHUNK_SIZE);
    
            /* read returns 0 when end of file is reached */
            while((error_read = read(infile_fd, ptr, CHUNK_SIZE)) != 0){
                    write(outfile_fd, ptr, CHUNK_SIZE);
            }
    
            close(infile_fd);
            close(outfile_fd);
            free(ptr);
    
            return 0;
    }
    Thanks,
    Livin

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    try
    Code:
            while((error_read = read(infile_fd, ptr, CHUNK_SIZE)) < 1 ){
    read returns -1 on error
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    From the documentation (emphasis mine)
    Quote Originally Posted by man 2 read
    RETURN VALUE
    On success, the number of bytes read is returned (zero indicates end of file), and the
    file position is advanced by this number. It is not an error if this number is smaller
    than the number of bytes requested; this may happen for example because fewer bytes are
    actually available right now (maybe because we were close to end-of-file, or because we
    are reading from a pipe, or from a terminal), or because read() was interrupted by a sig‐
    nal. On error, -1 is returned, and errno is set appropriately. In this case it is left
    unspecified whether the file position (if any) changes.
    It's possible you have an error reading, instead of end of file, so you may keep getting a return value of -1, which would cause you to keep calling read, which will probably keep returning -1. You should check if error_read <= 0 to stop. Then, after the loop, if it's less than 0, use perror to print a sensible error message as to why the read failed.

    Also, you have two problems with your writing. First, read might not read all the bytes you asked for, so you don't want to write CHUNK_SIZE bytes, instead you want to write error_read bytes. Second, you need to check the return value of write for error conditions and for short write counts (unlikely, but hey). Keep writing from where ever you left off in ptr if you didn't write all error_read bytes at once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM