PDA

View Full Version : Simple copy program hangs?



curlious
07-24-2004, 04:06 PM
This simple file copy program hangs and I don't know why. Help!


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

Salem
07-24-2004, 04:15 PM
No idea - works on my system.
How did you run it from the command line?

Hammer
07-24-2004, 04:54 PM
>>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...

curlious
07-24-2004, 05:00 PM
ok thanks I'll check it out and add some error checking.