Formatted Input Output In C Language
Formatted I/O:
For Example :- Program to read in a string and an integer from the keyboard, write them to a disk file and then read and display the file contents on screen.
CODE/PROGRAM/EXAMPLE
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp ;
char s[80] ;
int t ;
if ( ( fp = fopen( "test.dat", "w" ) ) == NULL )
{
puts( "Cannot open file test.dat") ;
exit(1) ;
}
puts( "Enter a string and a number") ;
scanf( "%s %d", s, &t );
fprintf( fp, "%s %d", s, t );
fclose( fp ) ;
if ( ( fp = fopen( "test.dat", "r" ) ) == NULL )
{
puts) "Cannot open file") ;
exit(1) ;
}
fscanf( fp, "%s %d" , s , &t ) ;
printf( "%s, %d\n", s, t ) ;
fclose( fp ) ;
}
Note : There are several I/O streams opened automatically at the start of every C program.
- stdin --- standard input ie. keyboard
- stdout --- standard output ie. screen
- stderr --- again the screen for use if stdout malfunctions
It is through these streams that the console functions we normally use operate. For example in reality a normal printf call such as
CODE/PROGRAM/EXAMPLE
printf( "%s %d", s, t ) ;
is in fact interpreted as
CODE/PROGRAM/EXAMPLE
fprintf( stdout, "%s %d", s, t ) ;
fread() and fwrite():
These two functions are used to read and write blocks of data of any type. Their prototypes are as follows where size_t is equivalent to unsigned.
Syntax
size_t fread( void *buffer, size_t num_bytes, size_t count, FILE *fp ) ;
size_t fwrite( const void *buffer, size_t num_bytes, size_t count, FILE *fp ) ;
where buffer is a pointer to the region in memory from which the data is to be read or written respectively, num_bytes is the number of bytes in each item to be read or written, and count is the total number of items ( each num_bytes long ) to be read/written. The functions return the number of items successfully read or written.
For Example :-
CODE/PROGRAM/EXAMPLE
#include <stdio.h>
#include <stdlib.h>
struct tag {
float balance ;
char name[ 80 ] ;
} customer = { 123.232, "John" } ;
void main()
{
FILE *fp ;
double d = 12.34 ;
int i[4] = {10 , 20, 30, 40 } ;
if ( (fp = fopen ( "test.dat", "wb+" ) ) == NULL )
{
puts( "Cannot open File" ) ;
exit(1) ;
}
fwrite( &d, sizeof( double ), 1, fp ) ;
fwrite( i, sizeof( int ), 4, fp ) ;
fwrite( &customer, sizeof( struct tag ), 1, fp ) ;
rewind( fp ) ; /* repositions file pointer to start */
fread( &d, sizeof( double ), 1, fp ) ;
fread( i, sizeof( int ), 4, fp ) ;
fread( &customer, sizeof( struct tag ), 1, fp ) ;
fclose( fp ) ;
}
NOTE : Unlike all the other functions we have encountered so far fread and fwrite read and write binary data in the same format as it is stored in memory so if we try to edit one these files it will appear completely garbled. Functions like fprintf, fgets, etc. read and write displayable data. fprintf will write a double as a series of digits while fwrite will transfer the contents of the 8 bytes of memory where the double is stored directly.
Random Access I/O:
The fseek() function is used in C to perform random access I/O and has the following prototype.
Syntax
int fseek ( FILE *fp, long num_bytes, int origin ) ;
where origin specifies one of the following positions as the origin in the operation
- SEEK_SET --- beginning of file
- SEEK_CUR --- current position
and where num_bytes is the offset in bytes to the required position in the file. fseek() returns zero when successful, otherwise a non-zero value.
For Example if we had opened a file which stored an array of integers and we wish to read the 50th value we might do the following
CODE/PROGRAM/EXAMPLE
fseek ( fp, ( 49 * sizeof( int ) ), SEEK_SET ) ;
fscanf( fp, "%d", &i ) ;
from anywhere in the program.