C++ Sequential input output operations
The file stream classes support a number of member functions for performing the input and output operation on files.
one pair of function put() and get() are designed for handling a single character at a time. Another pair of functions write() and read()
are use to write and read blocks of binary data.
put() and get() function
here put() write a single character to the associated stream. similarly the get() function reads a single character from the associated stream. in the below program show how these functions work on file. The program requests for a string. on recieve the string, The program writes it, character by character, to the file using the put() function in a for loop.
Note That the length of the string is used to terminate the for loop
The program then displays the contents of the file on the screen. it uses the function get() to fetch single character from the file and continues to do so until the (EOF) end of file condition is reached The character read from the file is displayed on the screen using the operator <<
I/O operations on character
<iostream>
<fstream>
<string.h >
main()
{
string[80];
std::cout << "Enter a string \n";
std::cin >> string;
len=strlen(string);
std::fstream file;
file.open("TEXT",std::iostream::in | std::iostream::out);
(int i=0;i < len;i++)
file.put(string[i]);
file.seekg(0);
ch;
(file)
{
file.get(ch);
std::cout << ch;
}
}
|
output
Enter a string
input : forton programming
output: forton programming
|
Note: we have used an fstream object to open the file. since an fstream object can handle bith the input and output simultaneously, we have opened the file in ios::in | ios::out
mode. after writing the file , we want to read the entire file and display its contents. since the file pointer has already moved to the end of the file, we must bring it back to the start of the file
this is done by the statement
write() and read() function
the function write() and read() unlike the function put() and get(), handled the data in binary form. This means that the values are stored in the disk file in the same format in which they are stored in the internal memory. in fig show how an int value 2594 is stored, in the binary and character format. an int takes two bytes to store its value in the binary form.
irrespective of its size but a 4 digit in will take four bytes to store it in the character form
The binary format is more accurate for storing the number as they are stored in the exact internal representation. there are no conversions while saving the data and therefore saving is much faster the binary input and output functions takes the following form
infile.read((char*)& v, sizeof(V));
outfile.write((char *)& V, sizeof((V));
|
These functions take two arguments, The first is the address of the variable V, and the secind is the length of that variable
in bytes. The address of the variable must be cast to type char* program below how these two functions are used to save an array of float number and then recover them for display on the screen.
I/O operations on binary files
< iostream>
<fstream>
<iomanip>
*filename="BINARY";
main()
{
height[4]={34.5,67.4,34.5,23};
std::ofstream outfile;
outfile.open(filename);
outfile.write((char *)& height, sizeof(height));
outfile.close();
int i;
(int i=0;i<4;i++)
height[i]=0;
std::ifstream infile;
infile.open(filename);
infile.read((char *) & height,sizeof(height));
(i=0;i<4;i++)
{
std::cout.setf(std::iostream::showpoint);
std::cout << std::setw(10) << std::setprecision(2) << height[i];
}
infile.close();
}
|
output is :
34.5 67.4 34.5
|