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

Specification: (16) (B) Write A C Program To Count The Number of Characters and Numbers of Lines in A File. Algorithm

The document describes a C program to count the number of characters and lines in a file. It includes the algorithm, flowchart and code for the program. The algorithm has 6 steps: 1) Declare variables, 2) Read filename, 3) Open file, 4) Read characters until EOF, incrementing the character and line counters, 5) Close file and display counts, 6) Stop. The code implements this by opening the file, getting characters with fgetc() and counting newlines and total characters. It prints and returns the number of characters and lines. Sample input/output shows it processing an existing file and handling a non-existent file.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

Specification: (16) (B) Write A C Program To Count The Number of Characters and Numbers of Lines in A File. Algorithm

The document describes a C program to count the number of characters and lines in a file. It includes the algorithm, flowchart and code for the program. The algorithm has 6 steps: 1) Declare variables, 2) Read filename, 3) Open file, 4) Read characters until EOF, incrementing the character and line counters, 5) Close file and display counts, 6) Stop. The code implements this by opening the file, getting characters with fgetc() and counting newlines and total characters. It prints and returns the number of characters and lines. Sample input/output shows it processing an existing file and handling a non-existent file.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

RAGHU INSTITUTE OF TECHNOLOGY

SPECIFICATION:
(16)(b) Write a c program to count the number of characters and numbers of lines in a file.
ALGORITHM:
Step1: Start
Step2: Declare file pointer fp, character ch, filename[20], and initialize noc=1, nol=0.
Step3: Read filename.
Step4:
Step 4.1: Open file fp in read mode.
Step 4.2: Condition (fp==NULL) prints data and stops if true, if false gets character from fp
and stores in ch.
Step 4.3: (ch!=EOF) is checked if true, noc is incremented
Step 4.3.1: (ch==\n) condition is checked if true nol is incremented.
Step 4.3.2: If the above step is true, then gets character from fp and stores in ch.
Step 4.3.3: Close the file fp.
Step5: Display number of characters and lines.
Step6: Stop

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Flowchart:

Start

Declare file pointer fp


Declare and initialize noc=1, nol=0, ch, filename

Read filename

Open the file in read mode

Fp==NULL

True

False
Read first character in
fp and store in ch

Cannot open file

Stop
False
Ch!=EOF

True

noc++

False

ch==\n
True

nol++
chfgetc fp

close file fp

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Number of character are:


Number of lines are:

Stop

PROGRAM
/*C program to count the number of characters and numbers of lines in a file.*/
Program name:

wk16b.c

/* Done By : C-Faculty
#include<stdio.h>
#include<curses.h>
int main()
{
int noc=0,nol=0;
FILE *fp;
char filename[25],ch;
clear();
printf(enter the file name);
gets(filename);
fp=fopen(filename,r);
if(fp==null)
{
printf(file cannot be opened);
return(0);
}
ch=fgetc(fp);
Department of Computer Science & Engg

Dated: 15/10/2013*/

RAGHU INSTITUTE OF TECHNOLOGY

while(ch!=EOF)
{
noc++;
if(ch=\n)
{
nol++;
}
ch=fgetc(fp);
}
fclose(fp);
printf(number of character are,%c,noc);
printf(number of lines are,%d,nol);
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 wk16b.c lcurses
Step 3: Now go for running the program by using the command
./a.out

hi.txt

Step 4: To create an executing file use the command


cc wk16b.c -curses o countchars

EXPECTED I/P AND O/P:


cat >> hi.txt // Already Content Existing File

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Sravan
Prasad
Radhika
Rajesh
number of character are:27
number of lines are: 4
ORIGINAL OUTPUT :
Output(1)
cat >> hi.txt // Already Content Existing File
Sravan
Prasad
Radhika
Rajesh
number of character are:27
number of lines are: 4
Output(2)
cat >> bii.txt // File Doesnt exists
file cannot be opened.
--xXx--

Department of Computer Science & Engg

You might also like