Guest User

Untitled

a guest
Jun 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. using namespace std;
  7.  
  8.  
  9.  
  10. int main (int argc, char * const argv[])
  11. {
  12. //file directors
  13. int fd_in;
  14. int fd_out;
  15. //file pointers
  16. char *outfile;
  17. char *infile;
  18. //Buffer and count for the reading and writing of the files.
  19. char buffer[1024];
  20. int count;
  21.  
  22. if(argc != 3)
  23. {
  24. cout <<"invalid number of arguments" << endl;
  25. }
  26.  
  27. else {
  28.  
  29. //This section initializes the infile and outfile and attempts to open it.
  30.  
  31. infile = argv[1];
  32. outfile = argv[2];
  33.  
  34. fd_in = open(infile,O_RDONLY);
  35.  
  36. //If the O_RDONLY flag fails this will let the user know.
  37. if(fd_in < 0)
  38. {
  39. cout<<"File did not open correctly."<<endl;
  40. }
  41.  
  42. else {
  43.  
  44. //These next two lines of code prime the files so that they can enter the loop and
  45. //complete the file copy.
  46. fd_out = open(outfile,O_CREAT | O_WRONLY | O_TRUNC,0666);
  47.  
  48. count = read(fd_in, (void*) buffer,sizeof(buffer));
  49.  
  50. write(fd_out,(void*)buffer,count);
  51.  
  52. while(count >= sizeof(buffer))
  53. {
  54. count = read(fd_in, (void*)buffer, sizeof(buffer));
  55. write(fd_out, (void*)buffer, count);
  56. }
  57. //close files
  58. close(fd_in);
  59. close(fd_out);
  60.  
  61. }
  62. }
  63.  
  64. return 0;
  65. }
  66.  
  67.  
  68.  
  69. //open file for input
  70. // = open(filename, O_RDONLY);
  71. //int fd_in; file desriptor. Gonna have to hand that integer
  72. //to any other system call that messes with the file.
  73. //if(fd_in<0) means open failed and there is an error
  74. //open file for output: fdout = open(filename, O_CREATE|O_TRUNC)
  75.  
  76. //opened the files
  77. //copy data
  78. //char buffer[1024]
  79. // count = read(fd_in,buf); or read(fd_in, (void*)buf,sizeof(buf));
  80. //write(fd_out, (void*)buf,count));
Add Comment
Please, Sign In to add comment