Detailed document flow and string flow _c language in C + + programming

Source: Internet
Author: User

C + + file stream classes and file stream objects
A file stream is a data stream that takes an external memory file as an input and output object. The output file stream is the data flowing from memory to the external memory file, and the input file stream is the data flowing from the external storage file to RAM. Each file stream has a memory buffer corresponding to it.

Distinguish between file flow and file concepts without mistaking the file stream for a stream composed of several files. The file stream itself is not a file, but only a stream of input and output objects as a file. To input and output to a disk file, you must implement it through a file stream.

Several file classes are defined in the I/O class Library of C + +, specifically for input and output operations on disk files.
In addition to the standard input and output stream classes IStream, Ostream, and iostream classes, there are 3 file classes for file operations:

    1. The Ifstream class, which is derived from the IStream class, and is used to support input from disk files.
    2. Ofstream class, derived from the Ostream class, to support output to disk files.
    3. The FStream class, derived from the Iostream class, that supports the input and output of disk files.

To input and output a disk file as an object, you must define an object for a file flow class, output data from memory to a disk file through a file flow object, or enter data from a disk file into memory from a file stream object.

In fact, in the use of standard equipment for the object of the input and output, but also to define the flow object, such as CIN, cout is the flow object, C + + is through the flow of input and output objects. Because Cin and cout have been defined in iostream.h, users do not need to define them. When you use disk files, you cannot define them in advance because they are different and must be defined by the user. In addition, operations on disk files are implemented through file stream objects (not cin and cout). File stream objects are defined by file flow classes, not with IStream and Ostream classes. You can create an output file stream object in the following ways:

  Ofstream outfile;


As with the Stream object cout-sample defined in the header file iostream, the object that outfile is Ofstream class (output file stream Class) is now defined in the program. But there is one question that has not been resolved: when defining cout, it has been associated with a standard output device (monitor), and now although an output file stream object has been established, it has not been specified to which disk file output it needs to be specified when it is used.

C + + reading and writing to string streams
The file stream is the data stream of the input output object with the external memory file, the string stream is not an object that is input or output with an external memory file, but an object that is input-and-output by an in-user-defined character array (string), that outputs data to an array of characters in memory, or reads data from a character array (string). A string stream is also called a memory stream.

The string stream also has a corresponding buffer, and the start Shireu buffer is empty. If you are storing data into a character array, as you insert data into the stream, the data in the stream buffer is incremented and the buffer is full (or a newline character), and the character array is stored together. If the data is read from a character array, the data in the character array is first sent to the stream buffer, and then the data is fetched from the buffer to be assigned to the variable.

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

File stream classes have Ifstream,ofstream and fstream, while 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 they are basically the same way of doing things. Writing data to an array of characters in memory is like writing data to a file, but there are 3 different points:
The output is not flowing to the external storage file, but to a memory space. Reads data from an in-memory storage space when it is entered. In a strict sense, this does not belong to the input and output, called Read and write more appropriate. Because the input output generally refers to the data transfer between computer memory and files outside the computer (external devices are also treated as files). But because the C + + string stream uses the C + + flow input and output mechanism, so often also uses the input and the output to express reads and writes the operation.
A string stream object is not associated with a file, but rather an array of characters in memory, so you do not need to open and close the file.
At the end of each file, there is a file terminator that indicates the end of the file. Instead of a corresponding end flag in the character array associated with the string stream, the user will specify a special character as the Terminator to write to the character array after writing all the data.

The string stream class does not have an open member function, so it is necessary to establish an association between the string stream and the character array by the given arguments when the string stream object is established. The problem is resolved by calling the constructor. The method and meaning of establishing a string stream object are as follows.
Create an output string stream object

The Ostrstream class provides a prototype for the constructor:

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


Buffer is a pointer to the first element of a character array, and n is the size of the specified stream buffer (either the same size as the character array, or different), the 3rd parameter is optional, and the default is Ios::out. You can use the following statement to establish an output string stream object and establish an association with a character array:

  Ostrstream Strout (ch1,20);

The function is to establish the output string stream object Strout and to associate Strout with the character array ch1 (outputting the data to the character array ch1 through a string stream), and the stream buffer size is 20.
Create an input string stream object

The Istrstream class provides two constructor functions with parameters, and 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 that is used to initialize the stream object (which makes the stream object associated with the character array). You can create an input string stream object with the following statement:

  Istrstream Strin (CH2);


The function is to establish the input string stream object Strin, and to CH2 all the data in the character array as input string stream.

  Istrstream Strin (ch2,20);


The stream buffer size is 20, so only the 20 characters in the character array ch2 are used as the contents of the input string stream.
Create an input-output string stream object

The Strstream class provides a prototype for the constructor:

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


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

  Strstream Strio (Ch3,sizeof (CH3), ios::in|ios::out);


The function is to establish the input-output string stream object, CH3 the input-output object with the character array, and 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 include the header file Strstream (in gcc, the header file Strstream).

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

#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", and};
  Char c[50]; User-defined character array
  ostrstream Strout (c,30);//Set up output string Stream, association with array c, buffer long
  for (int i=0;i<3;i++)//to character array C write student data
   strout<<stud[i].num<<stud[i].name<<stud[i].score;
  strout<<ends; Ends is the I/O operator of C + +, inserting a ' \\0 '
  cout<< ' array C: ' <<c<<endl; Display characters in array C of characters
}

The output on the monitor at run time is as follows:

Array C:
1001li781002wang89.51004fun90

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

2 specifies a stream buffer size of 30 bytes when establishing a string stream strout, which is permissible, unlike the size of the character array C, where 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 will happen to the operation? 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. Typically, 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, which is determined by the way of output. If you later want to read the data back to the corresponding variable in the program, you will have problems because you cannot separate two contiguous data. To resolve this problem, spaces can be artificially added 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. This allows the runtime to output:

  1001 Li 1002 Wang 89.5 1004 Fun 90


You can clearly separate the data when you read it again.

The [example] holds an integer in a character array C, spaced in space, requiring that they be placed in an integral array, sorted by size, and then stored back in character array C.

#include <strstream>
using namespace std;
int main ()
{
  char c[50]= "65-23-32 321";
  int a[10],i,j,t;
  cout<< "Array C:" <<c<<endl; Displays the string in the character array
  istrstream Strin (c,sizeof (c));//Create input Stream object Strin and associate with character array C for
  (i=0;i<10;i++)
   strin> >a[i]; Reads an integer from the character array C to the integral array a
  cout<< "array A:";
  for (i=0;i<10;i++)
   cout<<a[i]<< ""; Display integer array A elements
  cout<<endl;
  for (i=0;i<9;i++)///Group A for (
   j=0;j<9-i;j++) if (a[j]>a[j+1) with bubbling method
     {t=a[j];a[j]=a[j+1];a[ j+1]=t;}
  Ostrstream Strout (c,sizeof (c)); Set up output stream object Strout and associate with character array C for
   (i=0;i<10;i++)
     strout<<a[i]<< ""; Store integers in character array C
  strout<<ends;//join ' \\0 ' cout<< '
  array C: ' <<c<<endl; Display character array C return
  0;
}

The results of the operation are as follows:

Array C:12 34 65-23-32 33 61 99 321 32 (character array C original)
array a:12 65-23-32 321  (the contents of integral array a)
arr Ay c: -32–12 321  (final content of character array C)

A few notes on the string stream:
1 do not need to open and close the file when using a string stream.

2 reading data from a character array through a string stream is like reading data from a keyboard, you can read character data from a character array, or you can read integers, floating-point numbers, or other types of data. If you do not use a string stream, you can only access characters from a character array, rather than reading the data in the form of other types of data. This is the advantage of accessing character arrays with string streams, and is easy to use.

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

4 with the output string to the character array C write data, starting from the first address of the array, so update the contents of the array.

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

As you can see from the above description of the string stream, the character array associated with the string stream is equivalent to a temporary warehouse in memory, which can be used to hold various types of data (stored in ASCII form) and read back from it when needed. Its usage is equivalent to a standard device (monitor and keyboard), but a standard device cannot save data, and the contents of a character array can be exported at any time with ASCII characters. It is easier to use than the external storage file, no need to establish files (no need to open and close), access speed. But its lifecycle is the same as the module it is in, such as the main function, and the character array does not exist after the module's lifecycle ends. Therefore, only temporary storage space can be used.

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.