C + + Learning 50 reading and writing to string streams

Source: Internet
Author: User

A file stream is a stream of data that is input and output to an external memory file, a string stream that is not an object that is input and output as an external memory file, an object that is input and output in an in-memory user-defined character array (string), an array of characters to output to memory, or a character array (string) to read the data into. A string stream is also known as a memory stream.

The string stream also has a corresponding buffer, and the start stream buffer is empty. If you save data to a character array, as you insert data into the stream, the data in the stream buffer increases, and the buffer is full (or newline characters), and the character array is stored together. If you are reading data from a character array, first send the data in the character array to the stream buffer and then extract the data from the buffer to the relevant variable.

You can store characters in a character array, or you can hold integers, floating-point numbers, and other types of data. Before storing data into a character array, convert the data from binary form to ASCII code, then store in the buffer and then send it from the buffer to the character array. When reading data from a character array, the data in the character array is first sent to the buffer, and the ASCII code is converted to a binary form before being assigned to the variable. In summary, the data format in the stream buffer is the same as the character array. This is similar to the input and output of a standard device (keyboard and monitor), where the keyboard and monitor are input-and-output devices, and the in-memory data is converted to an ASCII format 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 form, converted to the binary form of the corresponding variable type before the variable is assigned, and then assigned to the variable. The input and output of the string stream, if not clear, can be inspired by the input and output of the standard device.

The file stream classes have Ifstream,ofstream and FStream, while the string stream classes have Istrstream,ostrstream and Strstream. Both the file stream class and the string stream class are derived classes of the Ostream,istream and iostream classes, so the methods for manipulating them are essentially the same. Writing data to a character array in memory is like writing data to a file, but with 3 different points:

1. The output data is not directed to the external memory file, but to a storage space in memory. The data is read from the in-memory storage space at input. In a strict sense, this does not belong to the input and output, known as read and write more appropriate. Because the input output generally refers to the data transfer between the computer's memory and a file outside the computer (which is also considered a file by the external device). However, because C + + 's string stream adopts the C + + flow input and output mechanism, it often uses input and output to express read and write operations.

2. The string stream object is not associated with a file, but an array of characters in memory, so you do not need to open and close the file.

3. Each file ends with a file terminator indicating the end of the file. The string stream does not have a corresponding end flag in the character array associated with it, and the user specifies a special character as the Terminator to write to the character array after it has been written to the entire data.

The string stream class does not have an open member function, so it is necessary to establish the association of a string stream with a character array by a given parameter when establishing a string stream object. That is, by calling the constructor to resolve the problem. The methods and meanings of creating a string stream object are as follows.

Creating an 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 (the general selection is the same as the size of the character array, or it can be different), and the 3rd parameter is optional and defaults to Ios::out mode. You can create an output string stream object with the following statement and associate it with a character array:
Ostrstream Strout (ch1,20);
The function is to establish an output string stream object Strout, and to associate Strout with a character array ch1 (through a string stream to output the data to a character array ch1), the stream buffer size is 20.

Creating an input string stream object

The Istrstream class provides two parameters with a constructor, which is the prototype:
Istrstream::istrstream (char *buffer);
Istrstream::istrstream (char *buffer,int N);
Buffer is a pointer to the first element of the character array, which is used to initialize the stream object (associating the stream object with the character array). You can create an input string stream object with the following statement:
Istrstream Strin (CH2);
The function is to create an input string stream object Strin, which will use 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 in the character array ch2 are used as the contents of the input string stream.

Creating an 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);
The function is to create an input-output string stream object, with the character array CH3 as the input 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 Istrstream, Ostrstream, and Strstream classes should be included in the program with the header file Strstream (in gcc, with the header file Strstream).

[Example 13.17] saves a set of data in a character array.

#include <strstream>using namespacestd;structstudent{intnum; Charname[ -]; floatscore;};intMain () {student stud[3]={1001,"Li", +,1002,"Wang",89.5,1004," Fun", -}; Charc[ -];//user-defined character arrayOstrstream Strout (c, -);//establishes the output string stream, associates with the array c, and the buffer length    for(intI=0;i<3; i++)//write a student's data to character array Cstrout<<stud[i].num<<stud[i].name<<Stud[i].score; Strout<<ends;//ends is the I/O operator for C + +, inserting a ' \\0 'cout<<"Array C:"<<c<<endl;//displays characters in the character array C}

The output from the runtime on the monitor is as follows:
Array C:
1001li781002wang89.51004fun90

These are the characters in character array C. can see:

These are the characters in character array C. can see:
1) The data in character array c is all characters stored in ASCII code, not data in binary form.

2) Specify a stream buffer size of 30 bytes when establishing the string stream Strout, which is different from the size of the character array C, which is allowed when the string stream can transmit up to 30 characters to the character array C. Consider: If you change the stream buffer size to 10 bytes, that is:
Ostrstream.strout (c, 10);
What happens when it runs? The stream buffer can hold only 10 characters, and the 10 characters are written to the character array C. The results displayed at run time are:
1001li7810

There are only 10 valid characters in the character array C. Generally, the size of the stream buffer is specified in the same size as the character array.

3) There is no space between the data in the character array C, and it is determined by the way the output is connected. 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 contiguous data. To solve this problem, you can manually add spaces to the output. Such as
for (int i=0;i<3;i++)
strout<< "<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score;
You should also modify the size of the stream buffer so that it can accommodate all of the contents and change to bytes. In this way, the runtime outputs:
1001 Li 1002 Wang 89.5 1004 Fun 90
You can clearly separate the data when you read it again.

[Example 13.18] in a character array C holds an integer, separated by a space, requires that they be placed in an integer array, then sorted by size, and then stored back in the character array C.

#include <strstream>using namespacestd;intMain () {Charc[ -]="65-23-32 321"; inta[Ten],i,j,t; cout<<"Array C:"<<c<<endl;//displaying strings in a character arrayIstrstream Strin (c,sizeof(c));//establishes the input stream object Strin and associates it with the character array C    for(i=0;i<Ten; i++) Strin>>a[i];//reads an integer from the character array c to an integer array acout<<"Array A:";  for(i=0;i<Ten; i++) cout<<a[i]<<" ";//Show integer array a each elementcout<<Endl;  for(i=0;i<9; i++)//sorting array A with foaming method       for(j=0;j<9-i;j++)         if(a[j]>a[j+1]) {T=a[j];a[j]=a[j+1];a[j+1]=T;} Ostrstream Strout (c,sizeof(c));//establishes the output stream object Strout and associates it with the character array C       for(i=0;i<Ten; i++) Strout<<a[i]<<" ";//Place an integer in the character array Cstrout<<ends;//join ' \\0 'cout<<"Array C:"<<c<<endl;//display character array C   return 0;}

A few notes on string flow:
1) You do not need to open and close the file with a string stream.

2) reading data from a character array through a string stream is like reading data from the keyboard, reading character data from a character array, or reading into integers, floating-point numbers, or other types of data. If you do not use a string stream, you can access the characters individually from the character array, not the data as other types of data. This is the advantage of using a string stream to access character arrays, which is convenient and flexible to use.

3) The program has established two string streams Strin and Strout, associated with the character array C. Strin gets the data from the character array C, strout the data to the character array. operate on the same character array, respectively. You can even read and write a character array cross, and the input string stream and the output string flow have flow pointers indicating the current position and not interfering with each other.

4) When writing data with an output string to a character array C, the contents of the array are updated, starting at the first address of the array.

5) The character array associated with a string stream is not necessarily an array defined specifically for a string stream, it is the same as a normal array of characters, and can be used for various other operations on the array.

With this introduction to the string flow, you can see that the character array associated with the string stream is equivalent to a temporary warehouse in memory, which can be used to store various types of data (in ASCII form) and read back from it when needed. It is used as a standard device (monitor and keyboard), but standard devices cannot hold data, and the contents of a character array can be output at any time with ASCII characters. It is easier to use than the external memory file and does not have to create files (no opening and closing) for fast access. But its life cycle is the same as the module in which it resides (such as the main function), and the character array does not exist after the life cycle of the module has ended. Therefore, it can only be used as a temporary storage space.

C + + Learning 50 reading and writing to string streams

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.