428 Programming and Data Structures
Exam ple
fcloseall ()
This function closes all the open files and returns number of files closed. It does not require any
argument.
13.4 FILE I/O
I. f p r i n t f () This function is used for writing characters, strings, integers, floats etc. to the file. It
contains one more parameter that is file pointer, which points the opened file. Here is a program that
illustrates the use of f p r in t f ( ) functions.
13.7 Write a program to open a text file and write some text using fprintfO function. Open
the file and verify the contents.
# include <stdio.h>
# include <conio.h>
main()
{
FILE *fp ;
char t e x t [30] ;
fp=fopen( "T ext. t x t " , "w ) ;
c l r s c r ( ) ;
printf ("Enter Text Here
gets (text);
fprintf (fp,"%s",text);
fclose(fp);
I
OUTPUT:
Enter Text Here: Have a nice day.
Explanation In the above program f prin t f () functions writes the string to the file pointed by f p.
The string is collected through gets ( ) function into character array name [3 0 ].
2. f scanf ( ) This function reads character, strings, integer, floats etc. from the file pointed by file
pointer. A program is illustrated below based on this.
13.8 Write a program to enter data into the text file and read the same. Use "w +" file mode.
Use fscanfO to read the contents of the file.
# include <stdio.h>
# include <conio.h>
main()
{
FILE *fp;
char text [15] ;
int age;
fpsfopen ("Text. txt", "w+") ;
clrscr();
Files 429
printf ("N a m etAG E n");
scanf("%s %d",textf&age);
fprintf (fp,"%s %d", text,age);
printf ("Nam et AGEti ");
fscanf(fp/'%s %df/,text,&age);
printf ("% st% dn", text,agt);
fclose(fp);
I
OUTPUT:
Name
AGE
AMIT
12
Name
AGE
AM IT 12
Explanation In the above program f scanf () reads the data from the file named " t e x t . t x t " .
3. g e tc ( ) This function reads a single character from the opened file and moves the file pointer. It
returns EOF, if end of file is reached.
13.9 Write a program to read the contents of the file using getc(0 function.
# include <stdio.h>
# include <process.h>
# include cconio.h>
void mainO
{
FILE ;
char c;
clrsc rO ;
f=fopen("list.txt","r");
if(f==NULL)
{
printf(‘‘ Cannot open file");
exit(l);
I
while <(c=getc(f))!=EOF)
printf ("%c",c);
fcloseff);
I
OUTPUT:
aman
akash
amit
ajay
ankit
430 Programming and Data Structures
Explanation In the above program the ge tc () reads character from the file " li s t . txt". Some text is
to be written before reading this file.
4. putc ( ) This function is used to write a single character into a file. If an error occurs it returns EOF.
13.10 V/rite a program to write some text into the file using putcO function.
# include <stdio.h>
# include <conio;h>
void mainO
{
int c;
FILE * fp ;
clrscr ();
printf ("n Enter Few Words to Exitn");
fp=fopen("words.doc", ”w ");
while ( (c=getchari))!='*')
putc(c,fp);
fcloseifp);
}
OUTPUT:
Enter Few Words to Exit
This is saved into the file *
Explanation The putc () function writes character reads through getchar () in the file "words. doc".
The user should enter ' * ' to stop reading character.
5. f g e tc ( ) This function is similar to g e t c () function. It also reads a character and increases the
file pointer position. If any error or end of file is reached it returns EOF.
13.11 Write a program to read a 'C' program file and count the following in the complete
program.
Ibtal number of statements.
Total number of included files.
Total number of blocks & brackets.
#include <stdio.h>
finclude <conio.h>
nain()
<
PILE * f s ;
int i*0/x /y,CBO,sb>0,b>>0;
clrscr()j
fs=fopen(“PRG2. C", "r");
if(fs=**NILL)
Files 431
{
printf ("n File opening error.");
exit(l);
while ((x=fgetc(fs))!=EOF)
r
switch(x)
{
case :
C++;
break;
case T :
sb++;
break;
case T :
b++;
break;
case '# ':
«++;
break;
I
fcloseffs);
printf ("n Summary o f 'C Program ");
printf ("=========================");
printf ( Total Statements : %d ",c+i);
printf ( Include Statements : %d",i);
printf C'n Total Blocks II :% d ",sb);
printf ("n Total Brackets ( ) : %d",b);
printf rM============ ============">;
}
OUTPUT:
Total Statements : 25
Include Statements : 4
Total Blocks {) : 5
Total Brackets 0 : 25
Explanation In the above program the f g etc ( ) function reads the file "p rg 2 . c" character by
character and returns the read character to variable ' x ' . The variable ' x ' is then passed to switch ()
case. Depending upon the contents of the variable 'x ' respective counter is increased in each case
statement. Thus, at last all summary is printed.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.81.201