Advertisement
gigba

copia_caratteri_da_un_file_ad_un_altro

Dec 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. /* Scrivere un programma in C che presi in input due file, denominati fileuno e
  2.  filedue contenenti caratteri minuscoli, copi il contenuto di filedue in coda a
  3.  fileuno, e successivamente crei un nuovo file, il cui nome è inserito
  4.  dall’utente, in cui sia copiato il contenuto di fileuno invertendo le
  5. minuscole con le maiuscole*/
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. FILE * check_open_file(char *, char *);
  12. void check_argument_main(int);
  13. void append_content(FILE *, FILE *);
  14. void copy_and_uppecase(FILE*,FILE*);
  15.  
  16. int main(int argc, char *argv[]){
  17. FILE *uno_txt, *due_txt, *tre_txt;
  18. char file3[30];
  19.     uno_txt=check_open_file("uno.txt","r");
  20.     due_txt=check_open_file("due.txt","a");
  21.     //check_argument_main(argc);
  22.     append_content(uno_txt, due_txt);
  23.     printf("Dammi il nome di un file sul quale copieremo e convertiremo\n"
  24.            "in maiscolo i caratteri copiati : ");
  25.     scanf("%s",file3);
  26.     due_txt=check_open_file("due.txt","r");
  27.     tre_txt=check_open_file(file3,"w");
  28.     printf("%s",file3);
  29.     copy_and_uppecase(due_txt,tre_txt);
  30.     return 0;
  31. }
  32. FILE *check_open_file(char *file_name, char *mode){
  33.     FILE *stream;
  34.     stream=fopen(file_name,mode);
  35.     if(stream==NULL){
  36.         printf("Sorry, we have a problem, can't open %s\n", file_name);
  37.         exit(EXIT_FAILURE);
  38.     }
  39.     return stream;
  40.  
  41. }
  42. void check_argument_main(int num){
  43.     if(num!=3){
  44.         printf("Sorry,there is an issue.\n");
  45.  
  46.         printf("There is a mismatch with the arguments or\n "
  47.                "you have inserted a wrong command\n");
  48.  
  49.         printf("The command format is the following:\n"
  50.                "'command name', 'argument 1', 'argument 2'");
  51.  
  52.         exit(EXIT_FAILURE);
  53.  
  54.     }
  55. }
  56.  
  57.  
  58. void append_content(FILE *f1, FILE *f2){
  59.     char c;
  60.     while((c=getc(f1))!=EOF)
  61.     putc(c,f2);
  62.     fclose(f1);
  63.     fclose(f2);
  64.  
  65. }
  66.  
  67. void copy_and_uppecase(FILE *f2,FILE *f3){
  68.     char ch;
  69.     while((ch=fgetc(f2))!=EOF){
  70.       ch = toupper(ch);
  71.       fputc(ch,f3);
  72.  
  73.     }
  74.     fclose(f2);
  75.     fclose(f3);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement