C-23
-
Upload
rushdi-shams -
Category
Education
-
view
49 -
download
0
description
Transcript of C-23
Lecture 23Lecture 23Version 1.0Version 1.0
More on Files More on Files (Part II)(Part II)
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Records in FileRecords in File
So far we have manipulated files So far we have manipulated files only with characters and strings.only with characters and strings.
What if you want to deal with What if you want to deal with numbers?numbers?
What if you want to deal with What if you want to deal with numbers/strings/characters all numbers/strings/characters all together?together?
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 We will use structure to combine
these dissimilar data types into a these dissimilar data types into a single data structuresingle data structure
We will use fprintf ( ) to write them We will use fprintf ( ) to write them into fileinto file
We will use fscanf ( ) to read from We will use fscanf ( ) to read from filefile
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Text & Binary FilesText & Binary Files
A text file contains only textual A text file contains only textual information like alphabets, digits information like alphabets, digits and special symbols and special symbols
In actuality the ASCII codes of these In actuality the ASCII codes of these characters are stored in text files characters are stored in text files
a binary file is merely a collection of a binary file is merely a collection of bytes bytes
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
Example of handling Example of handling binary filesbinary files
Remember the copy-paste program?Remember the copy-paste program? In that case, we handled only .TXT In that case, we handled only .TXT
filesfiles So, now we will modify the same So, now we will modify the same
program so that the program can program so that the program can handle binary files as well.handle binary files as well.
Don’t worry Don’t worry , it is almost the , it is almost the same program as that one!same program as that one!
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
The record I/O program that we did in The record I/O program that we did in an earlier section has two an earlier section has two disadvantages: disadvantages:
The numbers (basic salary) would The numbers (basic salary) would occupy more number of bytes- when the occupy more number of bytes- when the file is opened in text mode, each number file is opened in text mode, each number is stored as a character string. is stored as a character string.
If the number of fields in the structure If the number of fields in the structure increase writing structures using increase writing structures using fprintf( )fprintf( ), or reading them using , or reading them using fscanf( ), fscanf( ), becomes quite clumsy. becomes quite clumsy.
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Efficient way to read/write Efficient way to read/write recordsrecords
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
fwrite ( &e, sizeof ( e ), 1, fwrite ( &e, sizeof ( e ), 1, fp ) ; fp ) ;
first argument is the address of the first argument is the address of the structure to be written to the disk structure to be written to the disk
second argument is the size of the second argument is the size of the structure in bytes structure in bytes
third argument is the number of such third argument is the number of such structures that we want to write at structures that we want to write at one time one time
argument is the pointer to the file we argument is the pointer to the file we want to write to want to write to
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
fread ( )fread ( )
If we have reached the end of file, If we have reached the end of file, since since fread( ) fread( ) cannot read anything, cannot read anything, it returns a 0. By testing for this it returns a 0. By testing for this situation, we know when to stop situation, we know when to stop reading. reading.