Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- using namespace std;
- int main (int argc, char * const argv[])
- {
- //file directors
- int fd_in;
- int fd_out;
- //file pointers
- char *outfile;
- char *infile;
- //Buffer and count for the reading and writing of the files.
- char buffer[1024];
- int count;
- if(argc != 3)
- {
- cout <<"invalid number of arguments" << endl;
- }
- else {
- //This section initializes the infile and outfile and attempts to open it.
- infile = argv[1];
- outfile = argv[2];
- fd_in = open(infile,O_RDONLY);
- //If the O_RDONLY flag fails this will let the user know.
- if(fd_in < 0)
- {
- cout<<"File did not open correctly."<<endl;
- }
- else {
- //These next two lines of code prime the files so that they can enter the loop and
- //complete the file copy.
- fd_out = open(outfile,O_CREAT | O_WRONLY | O_TRUNC,0666);
- count = read(fd_in, (void*) buffer,sizeof(buffer));
- write(fd_out,(void*)buffer,count);
- while(count >= sizeof(buffer))
- {
- count = read(fd_in, (void*)buffer, sizeof(buffer));
- write(fd_out, (void*)buffer, count);
- }
- //close files
- close(fd_in);
- close(fd_out);
- }
- }
- return 0;
- }
- //open file for input
- // = open(filename, O_RDONLY);
- //int fd_in; file desriptor. Gonna have to hand that integer
- //to any other system call that messes with the file.
- //if(fd_in<0) means open failed and there is an error
- //open file for output: fdout = open(filename, O_CREATE|O_TRUNC)
- //opened the files
- //copy data
- //char buffer[1024]
- // count = read(fd_in,buf); or read(fd_in, (void*)buf,sizeof(buf));
- //write(fd_out, (void*)buf,count));
Add Comment
Please, Sign In to add comment