0% found this document useful (0 votes)
13 views2 pages

CS September 27

Uploaded by

milanroy34
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)
13 views2 pages

CS September 27

Uploaded by

milanroy34
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/ 2

nain.

c textoxt

Online C Compiler.
Code, Compile, Run and Debug C program online.
5 Write your code in this editor and press "Run" but ton to cmpile and execute it.

7 ***************************t*******t****t***tt****tt*tt *****t*t******ttt***t/
8

10
#include <stdio.h
#include <stdlib.h>
11 int main()

FILE file;
4
char
char ch[10]- "hello":
7
char str[10]-"world";
int characters, Words, 1ines;
mode
Open text. txt 1n
W
9
file
/*
topen"text.txt",
if file
"W");
20
Check opened successfully *
if (file == NULL)

CnUnable to open file. In"):


C"Please check if file exists and you have read privilege. In");

/*writing data to file


fprinti(file,"%s", ch)
29
felose(file);
P r i n t i n g to console
30
ile =fopen ("text.txt", "r"):
while ((r= getc (file)) != EOF)
putchar
fclose(Tile)
printt n");
5 /* Open text. txt in 'a" mode */
36 file fopen("text.txt", "a");
*appending to text.cx/
6

9
fputs(str.file);
41
fclose(file);
Open text. txt in 'r' mode *
42 file fopen("text.txt", "r");
printing to consOle
4
while ((r= fgetc(file)) 1=EOF)
putchar
fclose (file);
return 0,

input
hello

hel1 oworl1d
Program finiahed with exit code 0

Press ENTER exit console


to
|
main.c textbt result.brt

2 #include <stdio.h>
3 #include <stdlib.h>
4 int main()

FILE file, *fp;


char ch;
int characters, words, lines
10
Open text. txt in r mode
file = fopen("text. txt"
*
"r");

11
/*Check if file opened successfully */
if (file == NULL

printi("inUnable to open file. \n");


printi("Please check if file exists and you have read privilege.In")
16
*Logic to count characters, words and lines. *
8 characters Words lines 0;
19
while ((ch =fgetc(file)) I= EOF)
20-
characters+*
2 /* Check new line */
2 if
(ch
lines+
'\n'
I| ch =="N0
/* Check words7

if(ch
Words+
ch =t ch= An ll ch
Increment words
(characters 0)
and lines for last word

words
lines*
printing to console*/

printi(in )
printi("Total characters %d\n", characters);
printf("Total words
9 %d in", words):
%d \n", lines);
40 printt (Total lines
* Open result. txt in 'w' mode */
42 fp fopen ("result, txt", "W)
*writing the result to file */
4 fprint (fp, "Total characters %din Total words d An Total lines = %d\n ", char
45 * Close files */
46
fclose(file);
47 TCLose(Tp);
48 return 0;
49

input

Total charactera 1394


Total wo rds 419

Total lines

.Program finished with exit code 0


Press ENTER to exit console -|

You might also like