C++ Sequential input output operations

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
#include <iostream>
#include <fstream>
#include <string.h >
int main()
{
char string[80];
std::cout << "Enter a string \n";
std::cin >> string;
int len=strlen(string);
std::fstream file; // input and output stream
file.open("TEXT",std::iostream::in | std::iostream::out);
for (int i=0;i < len;i++)

file.put(string[i]); // put a character to file
file.seekg(0); // go to the start
char ch;
while (file)
{
file.get(ch); // get a character from file
std::cout << ch; // display it on screen
}
}

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

file.seekg(0);

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

binary and character format of an integer value picture

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
#include < iostream>
#include <fstream>
#include<iomanip>
const char *filename="BINARY";
int main()
{
float 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; for(int i=0;i<4;i++)
height[i]=0;
std::ifstream infile;
infile.open(filename);
infile.read((char *) & height,sizeof(height));
for(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

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise