Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. /*На вход программы поступают имена трех
  2. файловНеобходимо записать содержимое
  3. первых двух файлов в третий в вперемешку*/
  4. /* program to merge two files
  5. and stores their contents in another file*/
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. int main()
  11. {
  12.    FILE *fs1, *fs2, *ft;
  13.  
  14.    char ch, firstFile[20], secondFile[20], mergedFile[20];
  15.  
  16.    printf("Enter name of first file\n");
  17.    gets(fileFile);
  18.  
  19.    printf("Enter name of second file\n");
  20.    gets(secondFile);
  21.  
  22.    printf("Enter name of file which will store contents of two files\n");
  23.    gets(mergedFile);
  24.  
  25.    fs1 = fopen(firstFile,"r");
  26.    fs2 = fopen(secondFile,"r");
  27.  
  28.    if( fs1 == NULL || fs2 == NULL )
  29.    {
  30.       perror("Error ");
  31.       printf("Press any key to exit...\n");
  32.       getch();
  33.       exit(EXIT_FAILURE);
  34.    }
  35.  
  36.    ft = fopen(mergedFile,"w");
  37.  
  38.    if( ft == NULL )
  39.    {
  40.       perror("Error ");
  41.       printf("Press any key to exit...\n");
  42.       exit(EXIT_FAILURE);
  43.    }
  44.  
  45.    while( ( ch = fgetc(fs1) ) != EOF )
  46.       fputc(ch,ft);
  47.  
  48.    while( ( ch = fgetc(fs2) ) != EOF )
  49.       fputc(ch,ft);
  50.  
  51.    printf("\n\nTwo files were merged into %s file successfully.\n",mergedFile);
  52.  
  53.    fclose(fs1);
  54.    fclose(fs2);
  55.    fclose(ft);
  56.  
  57.    return 0;
  58. }
  59.  
  60. output
  61.  
  62. Enter name of first file
  63. firstFile.txt
  64. Enter name of second file
  65. secondFile.txt
  66. Enter name of file which will store contents of two files
  67. mergedFile.txt
  68. Two files were merged into mergedFile.txt successfully
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement