0% found this document useful (0 votes)
190 views

Specification: (16) (C) Write A C Program To Merge Two Files Into A Third File, The Names of The Files Must Be Entered

The document describes a C program that merges two files into a third file using command line arguments to specify the file names. The program declares file pointers, checks for invalid arguments or if the files can be opened, then reads the first file and writes it to the third file before doing the same with the second file. It provides the full source code, explains how to compile and run the program, and gives sample input/output and validation questions.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views

Specification: (16) (C) Write A C Program To Merge Two Files Into A Third File, The Names of The Files Must Be Entered

The document describes a C program that merges two files into a third file using command line arguments to specify the file names. The program declares file pointers, checks for invalid arguments or if the files can be opened, then reads the first file and writes it to the third file before doing the same with the second file. It provides the full source code, explains how to compile and run the program, and gives sample input/output and validation questions.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

RAGHU INSTITUTE OF TECHNOLOGY

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

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

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

CANT OPEN SOURCE


FILE

FALSE
TRUE

Stop

Open file fp3 in write mode


FALSE
fp3==NULL

TRUE

((ch=fgetc(f
p1))!=EOF
TRUE
Put Ch in fp3
Department of Computer Science & Engg

files cant be
opened

STOP

RAGHU INSTITUTE OF TECHNOLOGY

FASLE

((ch=fgetc(
fp2))!=EOF

Put Ch in fp3

FILES ARE SUCESSFULLY


APPENEDED

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;

Department of Computer Science & Engg

// wk16c.c
Dated: 15/10/2013*/

RAGHU INSTITUTE OF TECHNOLOGY

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

RAGHU INSTITUTE OF TECHNOLOGY

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

Step 4: To create an executing file use the command


cc wk16c.c -curses o appendfiles

EXPECTED I/P AND O/P:


cat >> hi.txt // Already Content Existing File
Sravan
Prasad
Radhika
Sujatha
cat >> bi.txt

// Already Content Existing File

Rajesh
Hemanth
Sravani
Nagalaxmi
cat >> hello.txt

// File Created with Empty Data

After Program Execution : cat hello.txt contains File hi.txt & bi.txt content.

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

ORIGINAL OUTPUT :
Output(1)
cat >> hi.txt // Already Content Existing File
Sravan
Prasad
Radhika
Sujatha
cat >> bi.txt

// Already Content Existing File

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.

VIVA VOCE QUESTIONS:


1. The maximum combined length of the command-line arguments including the spaces between
adjacent arguments is?
Ans: The size will be varying from one operating system to another.
2. According to ANSI specifications the correct way of declaring main when it receives
command-line arguments?

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Ans: int main(int argc, char *argv[])


3. What do the 'c' and 'v' in argv stands for?
Ans: 'c' means argument count 'v' means argument vector

--xXx--

Department of Computer Science & Engg

You might also like