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