Details on file streams and string streams in C++ programming

  • 2020-05-05 11:33:18
  • OfStack

C++ file stream class with file stream object
File stream is a data stream with external file as input and output object. The output file stream is the data flowing from the memory to the external file, and the input file stream is the data flowing from the external file to the memory. Each file stream has a memory buffer corresponding to it.

Distinguish between a file stream and the concept of a file, and do not mistake a file stream for a stream of several files. The file stream itself is not a file, but simply a stream with the file as input and output object. To input and output to disk files, you must do this through a file stream.

Several file classes are defined in C++ 's I/O class library, specifically for input-output operations on disk files.
In addition to the standard input-output stream classes istream, ostream, and iostream, there are three file classes for file manipulation:

The ifstream class, which is derived from the istream class, supports input from disk files. The ofstream class, which is derived from the ostream class, supports output to disk files. The fstream class, which is derived from the iostream class, supports input and output to disk files.

To input and output a disk file as an object, you must define a file stream object that outputs data from memory to a disk file, or from a disk file to memory.

In fact, in the input and output using standard devices as objects, we also need to define the flow object, such as cin, cout is the flow object, C++ is the input and output through the flow object. Since cin and cout are pre-defined in iostream.h, users do not need to define them themselves. When using disk files, due to the different circumstances, can not be defined in advance, must be defined by the user. In addition, operations on disk files are implemented through file stream objects (not cin and cout). File flow objects are defined in the file flow class, not in the istream and ostream classes. You can create an output file stream object using the following method:


  ofstream outfile;


Just as the stream object cout - was defined in the header iostream, the object outfile is now defined in the program as ofstream class (output file stream class). However, there is one unresolved problem: cout was associated with the standard output device (the display) when it was defined, and now an output file stream object has been created, but it is not specified to which disk file it outputs, which needs to be specified when it is used.

C++ read and write
to the string stream File stream based on CRT object files as input and output data stream, string outside flow is not save the file as input and output of the object, and memory user defined character array (string) as input and output of the object, the data output to the memory of the character array, or from a character array (string) to read the data. A string stream is also called a memory stream.

The string stream also has a buffer, which is empty at the beginning. If you store data into a character array, the data in the stream buffer grows as you insert data into the stream, and when the buffer is full (or a newline character is encountered), the data is stored in the character array together. If the data is read from a character array, the data from the character array is sent to the stream buffer, and then the data is extracted from the buffer and assigned to the relevant variable.

You can store characters in character arrays, as well as integers, floating point Numbers, and other types of data. Before storing data into the character array, the data is converted from binary form to the ASCII code, stored in a buffer, and then sent from the buffer to the character array. When reading from a character array, the data in the character array is sent to the buffer, and the ASCII code is converted to binary before being assigned to a variable. In short, the data format in the stream buffer is the same as the character array. The situation is similar to that of input and output of standard devices (keyboard and display), which are character-based input and output devices. The data in memory is converted to ASCII code form and sent to the output buffer before being output to the display. The data entered from the keyboard is entered into the input buffer in ASCII code form, converted to the binary form of the corresponding variable type before being assigned to the variable, and then assigned to the variable. For the case of the input/output of a string stream, if it is not clear, you can take inspiration from the input/output to a standard device.

The file stream classes are ifstream,ofstream, and fstream, while the string stream classes are istrstream,ostrstream, and strstream. Both the file stream class and the string stream class are derived from the ostream,istream, and iostream classes, so they operate in essentially the same way. Writing to an array of characters in memory is like writing to a file, but with three differences:
When output, the data does not flow to the external file, but to a storage space in memory. The data is read from the storage space in memory as input. In the strict sense, this is not input and output, called read and write is appropriate. Because I/o generally refers to the transfer of data between the computer's memory and files outside the computer (external devices are also considered files). But because C++ 's string stream USES C++' s stream input-output mechanism, it is also common to express read and write operations in terms of input and output.
The string stream object is not associated with a file, but with an array of characters in memory, so there is no need to open and close the file.
At the end of each file is a file terminator, indicating the end of the file. There is no end flag in the array of characters associated with the string stream, and the user specifies a special character as the end character to be written after all the data has been written to the array of characters.

The string stream class does not have an open member function, so when creating a string stream object, the string stream is associated with an array of characters with a given parameter. This problem is solved by calling the constructor. The method and meaning of creating a string flow object are as follows.
Creates the output string stream object

The prototype of the constructor provided by the ostrstream class is:
   


ostrstream::ostrstream(char *buffer,int n,int mode=ios::out);


buffer is a pointer to the first element of the character array, n is the size of the specified stream buffer (generally chosen to be the same size as the character array or different), the third parameter is optional, the default is ios::out. You can create an output string stream object and associate it with an array of characters with the following statement:


  ostrstream strout(ch1,20);

Creates the output string stream object strout and associates strout with the character array ch1 (which outputs data to the character array ch1 via the string stream) with a stream buffer size of 20.
Creates the input string stream object

The istrstream class provides two constructors with arguments. The prototype is


  istrstream::istrstream(char *buffer);
  istrstream::istrstream(char *buffer,int n);


buffer is a pointer to the first element of the character array and is used to initialize the stream object (to associate the stream object with the character array). The input string stream object can be created with the following statement:
 


  istrstream strin(ch2);


Creates the input string stream object strin and takes all the data in the character array ch2 as the content of the input string stream.


  istrstream strin(ch2,20);


The stream buffer size is 20, so only 20 characters from the character array ch2 are used as the contents of the input string stream.
Creates the input/output string stream object

The prototype of the constructor provided by the strstream class is


  strstream::strstream(char *buffer,int n,int mode);


You can create an input-output string stream object with the following statement:


  strstream strio(ch3,sizeof(ch3),ios::in|ios::out);


Function is to create the input and output string stream object, with the character array ch3 as the input and output object, the stream buffer size is the same as the array ch3.

The above string stream classes are defined in the header file strstream, so the program should include the header file strstream when using the istrstream, ostrstream, and strstream classes (in GCC, strstream).

Saves a set of data in an array of characters.


#include <strstream>
using namespace std;
struct student
{
  int num;
  char name[20];
  float score;
};
int main( )
{
  student stud[3]={1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90};
  char c[50]; // An array of user-defined characters 
  ostrstream strout(c,30); // Creates an output string stream , With an array of c associated , Buffer warden 
  for(int i=0;i<3;i++) // Directed character array c Write a student's data 
   strout<<stud[i].num<<stud[i].name<<stud[i].score;
  strout<<ends; //ends is C++ the I/O The operator , Insert a '\\0'
  cout<<"array c:"<<c<<endl; // Display array of characters c The characters in 
}

The output of the runtime on the monitor is as follows:


array c:
1001Li781002Wang89.51004Fun90

Those are the characters in the character array c. You can see:
1) the data in the character array c is all the characters stored in the ASCII code, not the data represented in binary form.

2) specify that the stream buffer size is 30 bytes when creating the string stream strout, which is different from the size of the character array c, which is allowed. In this case, the string stream can pass up to 30 characters to the character array c. Consider: if you change the stream buffer size to 10 bytes, that is:


ostrstream::ostrstream(char *buffer,int n,int mode=ios::out);
0


How does it work? The stream buffer can only hold 10 characters, which are written to the character array c. The result displayed at runtime is:


ostrstream::ostrstream(char *buffer,int n,int mode=ios::out);
1

There are only 10 valid characters in the character array c. The size of the stream buffer is generally specified to be the same as the size of the character array.

3) there is no space between the data in the character array c, which is connected into one piece, depending on the way the data is output. If you later want to read this data back to the corresponding variable in the program, you will have a problem because you cannot separate two adjacent data. To solve this problem, you can artificially add a space to the output. Such as


ostrstream::ostrstream(char *buffer,int n,int mode=ios::out);
2


You should also modify the size of the stream buffer so that it can hold all the content, now bytes. In this way, the runtime will output:


  1001 Li 78 1002 Wang 89.5 1004 Fun 90


The data is clearly separated when it is read in again.

An integer is stored in a character array c, separated by Spaces, and is required to be placed in an integer array, sorted by size, and returned to the character array c.


ostrstream::ostrstream(char *buffer,int n,int mode=ios::out);
4

The results are as follows:


ostrstream::ostrstream(char *buffer,int n,int mode=ios::out);
5

A few notes on string streams:
1) there is no need to open and close files when using string streams.

2) reading from a character array through a string stream is like reading from a keyboard. You can read character data from a character array, or integer, floating point, or other types of data. If you do not use a string stream, you can only access the characters one by one from an array of characters, rather than reading the data as other types of data. This is the advantage of using a string stream to access an array of characters.

3) two string streams strin and strout were established successively in the program, which were associated with the character array c. strin gets the data from the character array c, which passes the data to the character array. Operate on the same array of characters separately. You can even read and write across the array of characters. The input string stream and the output string stream have stream Pointers to indicate the current bit and do not interfere with each other.

4) when writing data from the output string to the c array of characters, it starts at the first address of the array, so the contents of the array are updated.

5) the array of characters associated with a string stream is not necessarily an array defined specifically for a string stream. It is no different from a normal array of characters and can be manipulated in a variety of other ways.

From the above introduction to string streams, you can see that the array of characters associated with a string stream is a temporary repository in memory for various types of data (in the form of ASCII) that can be read back as needed. Its usage is equivalent to that of a standard device (monitor and keyboard), but the standard device cannot hold the data, and the contents of the character array can be output at any time with the ASCII character. It is easier to use than the external file, do not need to create a file (do not need to open and close), access speed. However, its life cycle is the same as that of the module (such as the main function), and when the module's life cycle is over, the character array does not exist. Therefore, it can only be used as temporary storage space.


Related articles: