Raghu Institute of Technology

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

RAGHU INSTITUTE OF TECHNOLOGY

RAGHU INSTITUTE OF TECHNOLOGY


DEPARTMENT OF CSE
College Code: 3J

R13 Syllabus

C PROGRAMMING LAB MANUAL


=================================================
Exercise 16
a) Write a C program which copies one file to another.
b) Write a C program to count the number of characters and number of lines in a file.
c) Write a C Program t merge two files into a third file. The names of the files must be
entered using command line arguments.
=================================================
SPECIFICATION:
(16)(a) Write a C program which copies one file to another.
ALGORITHM:
Step1: Start
Step2: Declare ch, two file pointers fp1 and fp2.
Step3: Check the conditions :
Step 3.1: Check Condition (argc!=3) and prints data insufficient data and stops.
Step 3.2: If 3.1 Condition is true then Second condition (fp1==NULL (or) fp2==NULL) is
checked and print data Unable to open the file and stops if true, if false checks another condition.
Step 3.3:If condition 3.2 is true then Third condition (!feof(fp1)) is checked, if true, stores
first character in ch and puts ch in fp2 until condition is false and prints data if false.
Step4: Close the opened files fp1 and fp2.
Step 5: Stop

FLOWCHART:

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Start

Declare ch

Declare *fp1, *fp2

If
argc!=3
True

Insufficient data

Open files fp1,fp2 in read and


write mode

stop
fp1==null||fp2=
=null

True

Unable to open file

True
False

(!f eof(fp1))

Files successfully copied


True

Get first character of fp1 and Put ch


in fp2.

Close files fp1 and fp2

Stop

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/*C program to copy one file content to another file */
Program name:

// wk16a.c

/* Done By : C-Faculty
#include<stdio.h>
#include<curses.h>
int main(int argc,char *argv[]);
{
FILE *fp1,*fp2;
char ch;
clear();
if(argc!=3)
{
printf(insufficient data);
return(0);
}
fp1=fopen(argv[1] ,r);
fp2=fopen(arg[2] ,w);
if(fp1==NULL||fp2==NULL)
{
printf(files are not opening);
return(0);
}
while(!f eof(fp1))
{
ch=fgetc(fp1);
fputc(ch,fp2);
Department of Computer Science & Engg

Dated: 15/10/2013*/

RAGHU INSTITUTE OF TECHNOLOGY

}
printf(files successfully copied);
fclose(fp1);
fclose(fp2);
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 wk16a.c lcurses
Step 3: Now go for running the program by using the command
./a.out

hi.txt

bi.txt

Step 4: To create an executing file use the command


cc wk16a.c -curses o copyfiles

EXPECTED I/P AND O/P:


cat >> hi.txt // Already Content Existing File
Sravan
Prasad
Radhika
Rajesh
Cat >> bi.txt // File Created with Empty Data
After Program Execution : cat bi.txt contains File hi.txt content.
ORIGINAL OUTPUT :
Output(1)

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

cat >> hi.txt


Sravan
Prasad
Radhika
Rajesh
cat >> bi.txt
files successfully copied
Output(2)
cat >> hii.txt
cat >> bi.txt
files are not opening // Because file hii.txt doesnt exists.

--xXx--

Department of Computer Science & Engg

You might also like