2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Records in FileRecords in File
So far we have manipulated files only withSo far we have manipulated files only with
characters and strings.characters and strings.
What if you want to deal with numbers?What if you want to deal with numbers?
What if you want to deal withWhat if you want to deal with
numbers/strings/characters all together?numbers/strings/characters all together?
3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
The ProcedureThe Procedure
These are dissimilar data typesThese are dissimilar data types
We will use structure to combine these dissimilarWe will use structure to combine these dissimilar
data types into a single data structuredata types into a single data structure
We will use fprintf ( ) to write them into fileWe will use fprintf ( ) to write them into file
We will use fscanf ( ) to read from fileWe will use fscanf ( ) to read from file
6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Text & Binary FilesText & Binary Files
A text file contains only textual information likeA text file contains only textual information like
alphabets, digits and special symbolsalphabets, digits and special symbols
In actuality the ASCII codes of these charactersIn actuality the ASCII codes of these characters
are stored in text filesare stored in text files
a binary file is merely a collection of bytesa binary file is merely a collection of bytes
7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh
Example of handling binary filesExample of handling binary files
Remember the copy-paste program?Remember the copy-paste program?
In that case, we handled only .TXT filesIn that case, we handled only .TXT files
So, now we will modify the same program soSo, now we will modify the same program so
that the program can handle binary files as well.that the program can handle binary files as well.
Don’t worryDon’t worry , it is almost the same program, it is almost the same program
as that one!as that one!
9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh
The record I/O program that we did in anThe record I/O program that we did in an
earlier section has two disadvantages:earlier section has two disadvantages:
The numbers (basic salary) would occupy moreThe numbers (basic salary) would occupy more
number of bytes- when the file is opened in textnumber of bytes- when the file is opened in text
mode, each number is stored as a charactermode, each number is stored as a character
string.string.
If the number of fields in the structure increaseIf the number of fields in the structure increase
writing structures usingwriting structures using fprintf( )fprintf( ), or reading, or reading
them usingthem using fscanf( ),fscanf( ), becomes quite clumsy.becomes quite clumsy.
10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Efficient way to read/write recordsEfficient way to read/write records
11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh
fwrite ( &e, sizeof ( e ), 1, fp ) ;fwrite ( &e, sizeof ( e ), 1, fp ) ;
first argument is the address of the structure tofirst argument is the address of the structure to
be written to the diskbe written to the disk
second argument is the size of the structure insecond argument is the size of the structure in
bytesbytes
third argument is the number of such structuresthird argument is the number of such structures
that we want to write at one timethat we want to write at one time
argument is the pointer to the file we want toargument is the pointer to the file we want to
write towrite to
13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh
fread ( )fread ( )
If we have reached the end of file, sinceIf we have reached the end of file, since fread( )fread( )
cannot read anything, it returns a 0. By testingcannot read anything, it returns a 0. By testing
for this situation, we know when to stopfor this situation, we know when to stop
reading.reading.