Write a Program that Merges Lines Alternatively from 2 Files & Print Result.
C Program
#include <stdio.h>int main(){FILE *file1, *file2, *mergeFile;char fileName1[1000];char fileName2[1000];char mergeFileName[1000];char c1;char c2;printf("Enter first file name with full path: ");scanf("%s", fileName1);printf("Enter second file name with full path: ");scanf("%s", fileName2);printf("Enter merge file name with full path: ");scanf("%s", mergeFileName);//open files in read modefile1 = fopen(fileName1, "r");file2 = fopen(fileName2, "r");if (file1 == NULL || file2 == NULL){printf("Error in opening file \n");return -1;}//open file in write modemergeFile = fopen(mergeFileName, "w");if (mergeFile != NULL){printf("File created successfully!\n");}else{printf("Failed to create the file.\n");return -1;}char line[1000];c1 = fgetc(file1);c2 = fgetc(file2);int c = 0;//iterate till both we reach//end of any filewhile (c1 != EOF && c2 != EOF){if (c1 != EOF){ungetc(c1, file1);fgets(line, 1000, file1);fputs(line, mergeFile);c1 = fgetc(file1);}//put new lineif (c1 == EOF){putc('\n', mergeFile);}if (c2 != EOF){ungetc(c2, file2);fgets(line, 1000, file2);fputs(line, mergeFile);c2 = fgetc(file2);}//put new lineif (c2 == EOF){putc('\n', mergeFile);}}//if first file remainingwhile (c1 != EOF){ungetc(c1, file1);fgets(line, 1000, file1);fputs(line, mergeFile);c1 = fgetc(file1);}//if second file remainingwhile (c2 != EOF){ungetc(c2, file2);fgets(line, 1000, file2);fputs(line, mergeFile);c2 = fgetc(file2);}//close filesfclose(file1);fclose(file2);fclose(mergeFile);return 0;}
Input:
Enter first file name with full path: C:\Users\hello.txt
Enter second file name with full path: C:\Users\studentrecord.txt
Enter merge file name with full path: C:\Users\merge.txt
Output:
File created successfully!
Input File: hello.txt
1 2 3 4 5
Input File:studentrecord.txt
Ram Singh Rahul Raju Singh
Output File: merge.txt
1 Ram Singh 2 Rahul 3 Raju 4 Singh 5
No comments:
Post a Comment