ronyal

Untitled

Nov 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #define BUF_SIZE 1
  10. #define enter "\n"
  11.  
  12.  
  13. // gets 4 elements, filename ,in1, in2,out
  14. int main(int argc, char* argv[])
  15. {
  16.  
  17.     int input1_fd,input2_fd, output_fd,curfile = 1;
  18.     char buffer[BUF_SIZE];
  19.     bool flag = true ;
  20.  
  21.     /* Input and output file descriptors */
  22.     /* Number of bytes returned by read() and write() */
  23.     /* Character buffer */
  24.     /* Are src and dest file name arguments missing */
  25.     if(argc != 4)
  26.     {
  27.         printf ("arguments missing"); return 1;
  28.     }
  29.      
  30.     /*Create input file descriptor*/
  31.     input1_fd = open (argv[1],O_RDONLY);
  32.     if (input1_fd == -1)
  33.     {
  34.       perror("open");
  35.       return 2;
  36.     }
  37.     input1_fd = open (argv[2],O_RDONLY);
  38.     if (input2_fd == -1)
  39.     {
  40.       perror("open");
  41.       return 2;
  42.     }
  43.      
  44.     /*Create output file descriptor*/
  45.      
  46.     output_fd = open (argv[3],O_WRONLY | O_CREAT,0644);
  47.     if (output_fd == -1)
  48.     {
  49.       perror("open");
  50.       return 3;
  51.     }
  52.  
  53.     while(flag)
  54.     {
  55.             if(curfile == 1)
  56.                 read(input1_fd, buffer,1); // reads 1 char to buffer
  57.             else
  58.               {
  59.                 read(input2_fd, buffer,1);
  60.                 curfile = 2;
  61.               }          
  62.             if(strcmp(buffer,enter)!=0) // checks if not enter
  63.               write(output_fd,buffer,1); // writes to output
  64.             else
  65.               {
  66.                 write(output_fd,buffer,1); // writes to output
  67.                 //read(input2_fd, buffer,1) // reads 1 char to buffer
  68.                 curfile=2;
  69.               }
  70.    
  71.             if (!(feof(stdin)))
  72.                 {
  73.                     flag = false ;       
  74.                     printf("New text file generated");
  75.                     close(input1_fd);
  76.                     close(input2_fd);
  77.                     close(output_fd);
  78.                 }
  79.         //if(strcmp(buffer,EOF) == 0)
  80.             //  flag = false ;
  81.     }
  82. return 0;
  83. }
Add Comment
Please, Sign In to add comment