bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc087.c (2055B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 08 Feb 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program that merges lines alternately from two files and writes the results to a new file. Handle remaining lines if file sizes differ.
      9 */
     10 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(c) */
     11 
     12 /* This file is auto-generated by a bot. */
     13 /* This code is not compiled; it is for reference only. */
     14 
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <ctype.h>
     20 
     21 void create_sample_files();
     22 
     23 int main()
     24 {
     25     FILE *fp1, *fp2, *fp3;
     26     char line1[100], line2[100];
     27     char *res1, *res2;
     28 
     29     create_sample_files();
     30 
     31     fp1 = fopen("file1.txt", "r");
     32     fp2 = fopen("file2.txt", "r");
     33     fp3 = fopen("merge.txt", "w");
     34 
     35     if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
     36     {
     37         printf("File error.\n");
     38         exit(1);
     39     }
     40 
     41     // Read lines from both files
     42     res1 = fgets(line1, sizeof(line1), fp1);
     43     res2 = fgets(line2, sizeof(line2), fp2);
     44 
     45     while (res1 != NULL && res2 != NULL)
     46     {
     47         fputs(line1, fp3);
     48         fputs(line2, fp3);
     49         
     50         res1 = fgets(line1, sizeof(line1), fp1);
     51         res2 = fgets(line2, sizeof(line2), fp2);
     52     }
     53 
     54     // Append remaining lines from file 1
     55     while (res1 != NULL)
     56     {
     57         fputs(line1, fp3);
     58         res1 = fgets(line1, sizeof(line1), fp1);
     59     }
     60 
     61     // Append remaining lines from file 2
     62     while (res2 != NULL)
     63     {
     64         fputs(line2, fp3);
     65         res2 = fgets(line2, sizeof(line2), fp2);
     66     }
     67 
     68     printf("Files merged alternately into 'merge.txt'.\n");
     69 
     70     fclose(fp1);
     71     fclose(fp2);
     72     fclose(fp3);
     73 
     74     return 0;
     75 }
     76 
     77 void create_sample_files()
     78 {
     79     FILE *f1 = fopen("file1.txt", "w");
     80     FILE *f2 = fopen("file2.txt", "w");
     81     
     82     fprintf(f1, "File 1 - Line A\nFile 1 - Line B\nFile 1 - Line C\n");
     83     fprintf(f2, "File 2 - Line 1\nFile 2 - Line 2\nFile 2 - Line 3\nFile 2 - Line 4\n");
     84     
     85     fclose(f1);
     86     fclose(f2);
     87 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror