Simple copy program hangs?

This is a discussion on Simple copy program hangs? within the Linux Programming forums, part of the Platform Specific Boards category; This simple file copy program hangs and I don't know why. Help! Code: //simple copy file program #include <unistd.h> #include ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Simple copy program hangs?

    This simple file copy program hangs and I don't know why. Help!

    Code:
    //simple copy file program
    
    #include <unistd.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    
    #include <iostream>
    using std::cout;
    
    int main(int argc,char **argv)
    {
      // validate correct command line arguments
      if (argc!=3){
        cout << "Usage: copy_system <srcpath> <destpath>\n";
        exit(1);
      }
    
      
      char block[1024];// buffer for file copying
      int in, out;// source and destination file descriptors
      int nread;//bytes read from source
    
      // open source as read only
      if ((in=open(argv[1],O_RDONLY))==-1){
        cout << "Error opening file:" << argv[1] << "\n";
        exit(1);
      }
    
      //create destination file with owner read and write permissions set
      out=open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    
      // read data into buffer from source and copy to destination
      while ((nread=read(in,block,sizeof(block)))>0)
        write(out,block,nread);
    
      exit(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    No idea - works on my system.
    How did you run it from the command line?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>out=open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    You didn't error check this one, maybe there's a problem opening/writing to the file.

    Run it under a debugger to find out more...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    ok thanks I'll check it out and add some error checking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 02:46 PM
  2. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 08:54 AM
  3. need a simple program
    By Peter Griffin in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 03:23 PM
  4. I'm back! need help with a simple copy program
    By Rune Hunter in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 07:52 PM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21