Jeremiah_

SO SC-A

Sep 30th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/syscall.h>
  8.  
  9. #define MAXN 10001
  10.  
  11. int main()
  12. {   //var para guardar o caminho dos arquivos
  13.     char file1[MAXN], file2[MAXN];
  14.    
  15.     printf("Caminho do primeiro arquivo:"); //Lê o caminho
  16.     scanf(" %s", file1);
  17.  
  18.     printf("Caminho do segundo arquivo:");
  19.     scanf(" %s", file2);
  20.    
  21.     int id_file1 = open(file1, O_RDONLY); //System call pra abrir o arquivo.
  22.     int id_file2 = open(file2, O_RDONLY); //Open retorna o id do arquivo aberto.
  23.  
  24.     int bytes1, bytes2;
  25.  
  26.     //printf("%d %d\n", id_file1, id_file2); //Imprime os id's
  27.  
  28.     if( (bytes1 = read(id_file1, file1, MAXN) ) < 0 || ( bytes2 = read(id_file2, file2, MAXN)) < 0) //Lê o conteúdo do arquivo e
  29.     {                                                                                       //Se read retorna 0, houve algum erro
  30.         puts("ERROR");
  31.         exit(0);    
  32.     }    
  33.    
  34.     int id_ans = open("Conc.txt", O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, S_IRUSR); //Cria arquivo
  35.  
  36.     if( write( id_ans, file1, bytes1 ) < 0 || write(id_ans, file2, bytes2 ) < 0 ) //Escreve no arquivo criado
  37.     {
  38.         puts("ERROR1");
  39.         exit(0);
  40.     }
  41.    
  42.     close(id_file1); //Fecha arquivo
  43.     close(id_file2);
  44.     close(id_ans);
  45.    
  46.     puts("DONE");
  47. }
Advertisement
Add Comment
Please, Sign In to add comment