Files 421
fcloseffp);
printf Cn Contents read
fp*fopen("data.txt""r");
while Ofeoftfp))
printf <u%t",getc(fp));
J
rMrrpi nr*
K w AAmW At
Write data fe to stop press V: ABCDEFGHIJK.
Contents rtstd: ABCDEFGHIJK.
Explanation In the above program the file named "d a t a . t x t " is opened in written mode. The
characters are read from the keyboard and stored in variable 1 c '. Using f putc () the characters
are written to a file until ' . 9 (Dot) is pressed.
The same file is closed and then it is re-opened in read mode. On reopening of the file character
pointer sets to the beginning of the file. The contents of the file will be displayed on the screen using
g etc ().
3. a ( append) This mode opens a pre-existing file for appending data. If the file doesn't exist then
new file is opened i.e. if the file does not exist then the mode of "a " is same as V
Syntax
fp=fopen ("data.txt"/'a");
Here, if data , tx t file already exists, it will be opened. Otherwise a new file will be opened with
the same name.
13.2 Write a program to open a pre-existing file and add information at the end of file.
Display the contents of the file before and after appending.
# include <0tdio.h >
# include <conio*h>
# include <proces«.h>
void m ain()
{
fXLK *fp j
ehar c j
cXracrO;
printf ("Contents of file before appending Am");
fp*fopen(Mda ta. txt", V ') ;
422 Programming and Data Structures
while (lfeof(fp))
{
c=fgetc(fp);
printf ("% c",c );
}
fp=fopen ("data.txt","a");
if(fp==NULL)
{
printf ("File can not appended");
exit(l);
)
printf (" n Enter string to append:");
while (c l -.')
f
c=getche();
fputc(c,fp);
)
fcloseffp);
printf (" n Contents of file After appending :n");
fpafopenCdata. txt", "r");
while (!feof(fp))
I
c~fgetc(fp);
printf ("% c",c);
}
OUTPUT;
Contents of file before appending :
String is terminated with 'W .
Enter string to append :
This character is called as NULL character.
Contents of file After appending:
String is terminated with AO'.
This character is called as NULL character
4. w+ (Write + read) It searches for file, if found its contents are destroyed. If the file isn't found a
new file is created. Returns N ULL if fails to open the file. In this mode file can be written and read.
Files 423
Example
fp=fopen ("data. tx t", "w+") ;
In the above example d a ta . txt file is open for reading and writing operations.
133 Write a program to use w+ mode for writing and reading of a file.
# include <atdio.h>
# include <conio.h>
# include <proc*8S*h>
void main()
{
PILE * fp ;
char c='
c l r s c r ( ) ;
fp=fopen(“data.txt", "iv+");
if(fp ==N U L L)
{
printf ("Can not open file");
exit(l);
}
printf ("Write data & to stop press
whit*
i
csgetcheQ;
fpidc(c,fp);
}
rewind(fp);
printf ("n Contents read:");
while (ifeof(fp))
printf (" %c",getc(fp));
)
Q u rm iE
Write data & to stop press Y: ABCDEFGHIJK.
Contents read: ABCDEFGHIJK.
Explanation Instead of using separate read and write modes, one can use w+ mode to perform both
the operations. Hence in the above program w+ is used. At first writing operation is done. It is not
essential to close the file for reading the contents of it. We need to set the character pointer at the
beginning. Hence rewind () function is used. The advantage of using w+ is to reduce the number of
statements.
5. a + ( append + read ) In this mode file can be read and records can be added at the end of file.
424 Programming and Data Structures
Example
fp=fopen("data.txt"/'a+");
Here, data.txt is opened and records are added at the end of file without affecting the previous
contents.
13.4 Write a program to open a file in appendmode and add new records in it.
# include <stdio.h>
# include <conio.h>
# include <proce88.h>
void zoainO
{
FILE *fpf
char c « ' *;
c l r s c r ()/
fp=fopen(“data.txt","a+");
if (fp -~ N U L L )
I
printf ("Can not open file");
exitfl);
printf ( “Write data & to stop press
while (c!= .')
I
c=getche();
fputc(c,fp);
printf ("n Contents read
reimnd(fp);
while (!feof(fp))
printf ("%c",getc(fp));
D.umnQ
Write data & to stop press V : This is append and read mode.
Contents read: This is append and read mode.
Explanation In the above program a file named "d ata. txt" is opened in read and appends mode
(a+). If file does not exist a new file is created. Write operation is performed first and the contents are
read thereafter. Before reading character pointer is set at the beginning of file using rewind ( ) .
NOTE
1. In case read operation is done after write operation character pointer should be set at beginning
of the file us;ng rewind ().
Files 425
2. In case write/append operation is done after read operation. It is not essential to set the character
pointer at the beginning of file.
6. r+ (read + w rite ) This mode is used for both reading and writing. We can read and write
the record in the file. If the file does not exist, the compiler returns NULL to the file pointer. It can be
written as follows.
Exam ple
fp=fopen ( " d a t a .d a t " ," r + " );
i f ( fp==NULL)
p rin t f ("F ile not fou n d");
In the above example, d a ta. dat is opened for read and write operation. If f open () fails to open
the file it returns NULL. The i f statement checks the value of file pointer fp and if it contains
NULL a message is printed and the program terminates.
13.5 Write a program to open a file in read/ write mode in it. Read and write new information
in the file.
# include <stdio.h >
# include <conio.h>
# include <proce88.h>
void main()
{
FILE *fp ;
char cm'
c l r s c r ( ) ;
fp=fopen("data. txt", "r+ ");
if(fp==N U L L)
printf ("Can not open file");
exit(l);
1
printf ("n Contents read:");
while dfeoftfp))
prin tf("%c",getc(fp));
printf ("W rite data & to stop press V :");
while (c != V )
/
c=getche();
fputc(cffp);
I
..................Content has been hidden....................

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