2.4 Reading and Writing
read
To transfer data from a file to program variable(s).
write
To transfer data to a file from program variable(s) or constant(s).
The read and write operations are performed on the logical file with calls to library
functions.
For read, one or more variables must be supplied to the read function, to receive the
data from the file.
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 11
e h t o t d e i l p p u s e b t s u m ) s t n a t s n o c r o s e l b a i r a v s a ( s e u l a v e r o m r o e n o , e t i r w r o Fw
rite function, to provide the data for the file.
For unformatted transfers, the amount of data to be transferred must also be
supplied.
2.4.1 Read and Write Functions
Reading
The C++ read function is used to read data from a file for handle level access.
The read function must be supplied with (as an arguments):
o The source file to read from
o The address of the memory block into which the data will be stored
o The number of bytes to be read(byte count)
The value returned by the read function is the number of bytes read.
Read function:
Prototypes:
int read (int Handle, void * Buffer, unsigned Length);
Example:
read (Input, &C, 1);
Writing
The C++ write function is used to write data to a file for handle level access.
The handle write function must be supplied with (as an arguments):
o The logical file name used for sending data
o The address of the memory block from which the data will be written
o The number of bytes to be write
The value returned by the write function is the number of bytes written.
Write function:
Prototypes:
int write (int Handle, void * Buffer, unsigned Length);
Example:
write (Output, &C, 1);
2.4.2 Files with C Streams and C++ Stream Classes
For FILE level access, the logical file is declared as a pointer to a FILE (FILE *)
The FILE structure is defined in the stdio.h header file.
Opening
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 12
The C++ fopen function is used to open a file for FILE level access.
The FILE fopen function must be supplied with (as arguments):
o The name of the physical file
o The access mode
The value returned by the fopen is a pointer to an open FILE, and is assigned to the
file variable.
fopen function:
Prototypes:
FILE * fopen (const char* Filename, char * Access);
Example:
FILE * Input;
Input = fopen ("Daily.txt", "r");
The access mode should be one of the following strings:
r
Open for reading (existing file only) in text mode
r+
Open for update (existing file only)
w
Open (or create) for writing (and delete any previous data)
w+
Open (or create) for update (and delete any previous data)
a
Open (or create) for append with file pointer at current EOF (and keep any previous
data) in text mode
a+
Open (or create) for append update (and keep any previous data)
Closing
The C++ fclose function is used to close a file for FILE level access.
The FILE fclose function must be supplied with (as an argument):
o A pointer to the FILE structure of the logical file
The value returned by the fclose is 0 if the close succeeds, and &neq;0 if the close fails..
Prototypes:
int fclose (FILE * Stream);
Example:
fclose (Input);
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 13
Reading
The C++ fread function is used to read data from a file for FILE level access.
The FILE fread function must be supplied with (as an arguments):
o A pointer to the FILE structure of the logical file
o The address of the buffer into which the data will be read
o The number of items to be read
o The size of each item to be read, in bytes
The value returned by the fread function is the number of items read.
Prototypes:
size_t fread (void * Buffer, size_t Size, size_t Count, FILE * Stream);
Example:
fread (&C, 1, 1, Input);
Writing
The C++ fwrite function is used to write data to a file for FILE level access.
The FILE fwrite function must be supplied with (as an arguments):
o A pointer to the FILE structure of the logical file
o The address of the buffer from which the data will be written
o The number of items to be written
o The size of each item to be written, in bytes
The value returned by the fwrite function is the number of items written.
Prototypes:
size_t fwrite (void * Buffer, size_t Size, size_t Count, FILE * Stream);
Example:
fwrite (&C, 1, 1, Output);
2.4.3 Programs in C++ to Display the contents of a File
The first simple file processing program opens a file for input and reads it, character by
character, sending each character to the screen after it is read from the file. This program
includes the following steps
1. Display a prompt for the name of the input file.
2. Read the user’s response from the keyboard into a variable called filename.
3. Open the file for input.
4. While there are still characters to be read from the input file,
Read a character from the file;
Write the character to the terminal screen.
5. Close the input file.
Figures 2.2 and 2.3 are C++ implementations of this program using C streams and C++ stream
classes, respectively.In the C++ version, the call file.unsetf(ios::skipws) causes operator >> to include white
space (blanks, end-of-line,tabs, ans so on).
2.4.4 Detecting End of File
end-of-file
A physical location just beyond the last datum in a file.
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 15
The acronym for end-of-file is EOF.
When a file reaches EOF, no more data can be read.
Data can be written at or past EOF.
Some access methods set the end of file flage after a read reaches the end of file
position.
Other access methods set the end of file flag after a read attempts to read beyond the end of file
position.
Detecting End of File
The C++ feof function is used to detect when the file pointer of an fstream is past end of
file..
The FILE feof function has one argument.
o A pointer to the FILE structure of the logical file
The value returned by the feof function is 1 if end of file is true and 0 if end of file is
false.
Prototypes:
int feof (FILE * Stream);
Example:
if (feof (Input))
cout << "End of File\n";
In some languages, a function end_of_file can be used to test for end-of-file. The OS
keeps track of read/write pointer. The end_of_file function queries the system to see whether the
read/write pointer has moved past the last element in the file.
To transfer data from a file to program variable(s).
write
To transfer data to a file from program variable(s) or constant(s).
The read and write operations are performed on the logical file with calls to library
functions.
For read, one or more variables must be supplied to the read function, to receive the
data from the file.
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 11
e h t o t d e i l p p u s e b t s u m ) s t n a t s n o c r o s e l b a i r a v s a ( s e u l a v e r o m r o e n o , e t i r w r o Fw
rite function, to provide the data for the file.
For unformatted transfers, the amount of data to be transferred must also be
supplied.
2.4.1 Read and Write Functions
Reading
The C++ read function is used to read data from a file for handle level access.
The read function must be supplied with (as an arguments):
o The source file to read from
o The address of the memory block into which the data will be stored
o The number of bytes to be read(byte count)
The value returned by the read function is the number of bytes read.
Read function:
Prototypes:
int read (int Handle, void * Buffer, unsigned Length);
Example:
read (Input, &C, 1);
Writing
The C++ write function is used to write data to a file for handle level access.
The handle write function must be supplied with (as an arguments):
o The logical file name used for sending data
o The address of the memory block from which the data will be written
o The number of bytes to be write
The value returned by the write function is the number of bytes written.
Write function:
Prototypes:
int write (int Handle, void * Buffer, unsigned Length);
Example:
write (Output, &C, 1);
2.4.2 Files with C Streams and C++ Stream Classes
For FILE level access, the logical file is declared as a pointer to a FILE (FILE *)
The FILE structure is defined in the stdio.h header file.
Opening
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 12
The C++ fopen function is used to open a file for FILE level access.
The FILE fopen function must be supplied with (as arguments):
o The name of the physical file
o The access mode
The value returned by the fopen is a pointer to an open FILE, and is assigned to the
file variable.
fopen function:
Prototypes:
FILE * fopen (const char* Filename, char * Access);
Example:
FILE * Input;
Input = fopen ("Daily.txt", "r");
The access mode should be one of the following strings:
r
Open for reading (existing file only) in text mode
r+
Open for update (existing file only)
w
Open (or create) for writing (and delete any previous data)
w+
Open (or create) for update (and delete any previous data)
a
Open (or create) for append with file pointer at current EOF (and keep any previous
data) in text mode
a+
Open (or create) for append update (and keep any previous data)
Closing
The C++ fclose function is used to close a file for FILE level access.
The FILE fclose function must be supplied with (as an argument):
o A pointer to the FILE structure of the logical file
The value returned by the fclose is 0 if the close succeeds, and &neq;0 if the close fails..
Prototypes:
int fclose (FILE * Stream);
Example:
fclose (Input);
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 13
Reading
The C++ fread function is used to read data from a file for FILE level access.
The FILE fread function must be supplied with (as an arguments):
o A pointer to the FILE structure of the logical file
o The address of the buffer into which the data will be read
o The number of items to be read
o The size of each item to be read, in bytes
The value returned by the fread function is the number of items read.
Prototypes:
size_t fread (void * Buffer, size_t Size, size_t Count, FILE * Stream);
Example:
fread (&C, 1, 1, Input);
Writing
The C++ fwrite function is used to write data to a file for FILE level access.
The FILE fwrite function must be supplied with (as an arguments):
o A pointer to the FILE structure of the logical file
o The address of the buffer from which the data will be written
o The number of items to be written
o The size of each item to be written, in bytes
The value returned by the fwrite function is the number of items written.
Prototypes:
size_t fwrite (void * Buffer, size_t Size, size_t Count, FILE * Stream);
Example:
fwrite (&C, 1, 1, Output);
2.4.3 Programs in C++ to Display the contents of a File
The first simple file processing program opens a file for input and reads it, character by
character, sending each character to the screen after it is read from the file. This program
includes the following steps
1. Display a prompt for the name of the input file.
2. Read the user’s response from the keyboard into a variable called filename.
3. Open the file for input.
4. While there are still characters to be read from the input file,
Read a character from the file;
Write the character to the terminal screen.
5. Close the input file.
Figures 2.2 and 2.3 are C++ implementations of this program using C streams and C++ stream
classes, respectively.In the C++ version, the call file.unsetf(ios::skipws) causes operator >> to include white
space (blanks, end-of-line,tabs, ans so on).
2.4.4 Detecting End of File
end-of-file
A physical location just beyond the last datum in a file.
FILE STRUCTURES 10IS63
DEPT OF ISE, SJBIT Page 15
The acronym for end-of-file is EOF.
When a file reaches EOF, no more data can be read.
Data can be written at or past EOF.
Some access methods set the end of file flage after a read reaches the end of file
position.
Other access methods set the end of file flag after a read attempts to read beyond the end of file
position.
Detecting End of File
The C++ feof function is used to detect when the file pointer of an fstream is past end of
file..
The FILE feof function has one argument.
o A pointer to the FILE structure of the logical file
The value returned by the feof function is 1 if end of file is true and 0 if end of file is
false.
Prototypes:
int feof (FILE * Stream);
Example:
if (feof (Input))
cout << "End of File\n";
In some languages, a function end_of_file can be used to test for end-of-file. The OS
keeps track of read/write pointer. The end_of_file function queries the system to see whether the
read/write pointer has moved past the last element in the file.