Specification: (16) (C) Write A C Program To Merge Two Files Into A Third File, The Names of The Files Must Be Entered
Specification: (16) (C) Write A C Program To Merge Two Files Into A Third File, The Names of The Files Must Be Entered
SPECIFICATION:
(16)( c) Write a c program to merge two files into a third file, the names of the files must be entered
using command line arguments.
ALGORITHM:
Step1: Start
Step2: Declare file pointers fp1,fp2, fp3 and ch.
Step3: Check the conditions :
Step 3.1: Condition (argc!=4) and prints data and stops if true, if false opens the file fp1in read
mode, fp2 in read mode and fp3 in write mode respectively.
Step 3.2: (fp1==NULL (or) fp2==NULL) is checked and print data and stops if true, if false
checks another condition.
Step3.3: (fp3==NULL) is checked and print data and stops if true, if false checks another
condition.
Step 3.4: (!feof(fp1)) is checked, if true, stores first character in ch and puts ch in fp3 until
condition is false and prints data if false.
Step3.5: (!feof(fp2)) is checked, if true, stores first character in ch and puts ch in fp3 until
condition is false and prints data if false.
Step4: Close the opened files fp1, fp2 and fp3.
Step5: Stop
FLOWCHART
Start
Declare ch
Declare file pointers fp1,fp2,fp3
TRUE
argc!=3
FALSE
Insufficient data
Open file fp1 and fp2 in read
mode
Stop
TRUE
fp1==NULL
&
fp2==NULL
FALSE
TRUE
Stop
TRUE
((ch=fgetc(f
p1))!=EOF
TRUE
Put Ch in fp3
Department of Computer Science & Engg
files cant be
opened
STOP
FASLE
((ch=fgetc(
fp2))!=EOF
Put Ch in fp3
CLOSE FILES
FP1 , FP2,FP3
stop
PROGRAM
/* C Program to merge two files into a third file, the names of the files must be entered using
command line arguments.*/
Program name:
/* Done By : C-Faculty
#include<stdio.h>
#include<curses.h>
int main(int argc,char *argv[])
{
FILE *fp1,*fp2,*fp3;
// wk16c.c
Dated: 15/10/2013*/
char ch;
clear();
if(argc!=4)
{
printf(insufficient data);
return(0);
}
fp1=fopen(argv[1],r);
fp2=fopen(argv[2],r);
if(fp1==NULL||fp2==NULL)
{
printf(files are not opening);
return(0);
}
fp3=fopen(argv[3],w);
if(fp3==NULL)
{
printf(files cant be opened);
return(0);
}
while(((ch=fgetc(fp1))!=EOF)
fputc(ch,fp3);
while(((ch=fgetc(fp2))!=EOF)
fputc(ch,fp3);
printf(files are appended successfully);
fclose(fp3);
fclose(fp2);
Department of Computer Science & Engg
fclose(fp1);
return(0);
}
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the program and
quit)
Step 2: Now compile the program by using the following command
cc wk16c.c lcurses
Step 3: Now go for running the program by using the command
./a.out
hi.txt
bi.txt hello.txt
Rajesh
Hemanth
Sravani
Nagalaxmi
cat >> hello.txt
After Program Execution : cat hello.txt contains File hi.txt & bi.txt content.
ORIGINAL OUTPUT :
Output(1)
cat >> hi.txt // Already Content Existing File
Sravan
Prasad
Radhika
Sujatha
cat >> bi.txt
Rajesh
Hemanth
Sravani
Nagalaxmi
files are appended successfully
Output(2)
cat >> hi.txt // Already Content Existing File
Sravan
Prasad
Radhika
Sujatha
cat >> biii.txt // File doesnt exists
file cannot be opened.
--xXx--