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 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 = 1;
- char buffer[BUF_SIZE];
- bool flag = true ;
- /* Input and output file descriptors */
- /* 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;
- }
- /*Create input file descriptor*/
- input1_fd = open (argv[1],O_RDONLY);
- if (input1_fd == -1)
- {
- perror("open");
- return 2;
- }
- input1_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)
- {
- if(curfile == 1)
- read(input1_fd, buffer,1); // reads 1 char to buffer
- else
- {
- read(input2_fd, buffer,1);
- curfile = 2;
- }
- if(strcmp(buffer,enter)!=0) // checks if not enter
- write(output_fd,buffer,1); // writes to output
- else
- {
- write(output_fd,buffer,1); // writes to output
- //read(input2_fd, buffer,1) // reads 1 char to buffer
- curfile=2;
- }
- if (!(feof(stdin)))
- {
- flag = false ;
- printf("New text file generated");
- close(input1_fd);
- close(input2_fd);
- close(output_fd);
- }
- //if(strcmp(buffer,EOF) == 0)
- // flag = false ;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment