426 Programming and Data Structures
Q u m m
Contents read: Help me.
Write data & to stop pressI am in trouble.
Explanation In the above example file is opened in read and write mode ( r +). The ge tc () function
reads the contents of file which is printed through p rin tf () function. The getche () function reads
character from the keyboard and the read characters are written to file using f putc () function.
B Binary M odes
1. wb (w rite) This mode opens a binary file in write mode.
Example
fp= fopen ( "d a ta . d a t" , " wb") ;
Here, d ata. dat file is opened in binary mode for writing.
2. rb (read) This mode opens a binary file in read mode.
Example
fp =fo p e n ("d a ta .d a t", " r b " ) ;
Here, d a ta. dat file is opened in binary mode for reading.
13.6 Write a program to open a file for- read/ write operation in binary mode. Read
write new information in the file.
# include <stdio.h>
# include cconio.h>
# include <process.h>
void main()
{
FILE *fp ;
char c='
c l r s c r ( ) ;
data.dat,"w b ");
if(fp = = N U L L )
printf ("Can not open file");
exittt);
I
printf ("W rite data & to stop press
vhile (c l -.’)
{
c-getcheO;
fput.(J,fp);
fcloseifp);
fp-fopen(“da ta.dat","rb");
printf ("n Contents read:");
I
Files 427
while (Ifeoflfp))
printf ("%c",getcifp));
I
Explanation This program is the same as the one explained earlier and the only difference is that the
file-opening mode is binary
3. ab (append) This mode opens a binary file in append mode i.e. data can be added at the end
of file.
Example
fp=fopen( "d a ta . d a t" , "a b " ) ;
Here, "d a ta . dat" file is opened in append mode.
4. r+b (read + write) This mode opens a pre-existing file in read and write mode i.e. file can be
read and written.
Example
fp = fo p e n ("d a t a .d a t ","r + b " );
Here, the file "d a ta . dat" is opened for reading and writing in binary mode.
5. w+b (read + w rite ) This mode creates a new file in read and write mode.
Exam ple
fp=fopen( "d a ta .d a t "#"w+b");
Here, the file "da ta. dat" is created for reading and writing in binary mode.
6. a+b (append+ w rite ) This mode opens a file in append mode i.e. data can be written at the
end of file. If file does not exist a new file is created.
Example
fp = fo p e n ("d a t a .d a t","a + b ");
Here, the file "D ata. dat" is opened in append mode and data can be written at the end of file.
b. Reading a file Once the file is opened using f open ( ) , it's contents are loaded into the memory
(partly or wholly). The pointer points to the very first character of the file. The f getc () function is
used to read the contents of the file. The syntax for f getc () is as follows.
c h = fg e tc (fp );
Where, f ge tc () reads the character from current pointer position and advances the pointer position
so that the next character is pointed. The variable ch contains the character read by f ge tc ( ) . There are
also other functions to read the contents of file, which are explained in next pages.
C. Closing a File The file that is opened from the f open () should be closed after the work is over
i.e. we need to close the file after reading and writing operations are over.
Example
flco s e ( f p ) ;
This statement closes the file associated with file pointer f p. This function closes one file at a time. To
close one or more files at a time the function f c lo s e a ll () is used.
..................Content has been hidden....................

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