Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdbool.h>
- #define BUF_SIZE 2
- #define minus "-1"
- #define enter "\n"
- // gets 4 elements, filename ,in1, in2,out
- int main(int argc, char* argv[])
- {
- int input1_fd,input2_fd, output_fd,curfile = 2 , enOrMinus=0;
- char buffer[BUF_SIZE];
- bool flag = true ,eof1 = true, eof2 = true ,lflag = true;
- /* Number of bytes returned by read() and write() */
- /* Character buffer */
- /* Are src and dest file name arguments missing */
- if(argc != 4)
- {
- printf ("arguments missing");
- return 1;
- }
- /* Input and output file
- /*Create input file descriptor*/
- input1_fd = open (argv[1],O_RDONLY);
- if (input1_fd == -1)
- {
- perror("open");
- return 1;
- }
- input2_fd = open (argv[2],O_RDONLY);
- if (input2_fd == -1)
- {
- perror("open");
- return 2;
- }
- /*Create output file descriptor*/
- output_fd = open (argv[3],O_WRONLY | O_CREAT,0644);
- if (output_fd == -1)
- {
- perror("open");
- return 3;
- }
- while(flag) // while we still have tx1 and tx2
- {
- switch (curfile) //checks what file we are in
- {
- case 1 :
- {
- //printf("curfile is = %d\n",curfile);
- read(input1_fd, buffer,1); // reads 1 char to buffer
- if(strcmp(buffer,enter) == 0) // checks if enter
- enOrMinus=1;
- if(strcmp(buffer,minus) == 0) // checks if enter
- enOrMinus=2;
- switch(enOrMinus) // checks if enter
- {
- case 1 :
- if(eof2) // enter and tx2 not empty
- {
- curfile = 2;
- lflag = false;
- write(output_fd,buffer,1); // writes enter to output
- }
- break; // break enter
- case 2 : // end of for file 1
- {
- eof1 = false;
- if(eof2) //checks stile have file 2
- {
- curfile = 2;
- lflag = false;
- }
- break; // break minus
- }
- }// end switch(buffer)
- }// end case 1
- break; // break case 1
- case 2 :
- {
- // printf("%d\n",curfile);
- read(input1_fd, buffer,1); // reads 1 char to buffer
- if(strcmp(buffer,enter) == 0) // checks if enter
- enOrMinus=1;
- if(strcmp(buffer,minus) == 0) // checks if enter
- enOrMinus=2;
- switch(enOrMinus) // checks if enter
- {
- case 1 :
- if(eof1) // enter and tx1 not empty
- {
- curfile = 1;
- lflag = false;
- write(output_fd,buffer,1); // writes enter to output
- }
- break; // end enter
- case 2 : // end of for file 2
- {
- eof2 = false;
- if(eof1) //checks stile have file 1
- {
- curfile = 1;
- lflag = false;
- }
- }
- break; // end minus
- }// end switch(buffer)
- } // end case2
- break; // end case 2
- }// end switch(curfile)
- // chesks if a letter
- if((strcmp(buffer,enter) !=0 && ( strcmp(buffer,minus) !=0)))
- {
- write(output_fd,buffer,1); // writes enter to output
- }
- if (eof1 == false && eof2 == false)
- {
- flag=false;
- printf("output file generated succssesfuly");
- close(input1_fd);
- close(input2_fd);
- close(output_fd);
- }
- } // end while loop
- }// end main
Advertisement
Add Comment
Please, Sign In to add comment