Sstream, strstream, and fstream have two types of string streams in C ++, also known as array I/O streams, which are defined in sstream,
Another type is defined in strstream.
They basically implement the same thing.
Strstream contains
Class strstreambuf;
Class istrstream;
Class ostrstream;
Class strstream;
They are written based on the C-type string char *.
Sstream contains
Class istringstream;
Class ostringstream;
Class stringbuf;
Class stringstream;
Class .......
They are written based on STD: String
Therefore, ostrstream: STR () returns a char * string.
Ostringstream: STR () returns a string of the STD: string type.
Pay attention to the differences between the two. Generally, STD: string is recommended.
Of course, to maintain compatibility with C, using strstream is also a good choice.
But remember that, although strstream is still part of the C ++ language standard, it has been declared as "deprecated" by the C ++ standard, that is, it is no longer used, maybe it will be useless in the future.
Let's first introduce sstream.
// The stringstream uses spaces as the boundary and must contain the sstream header file.
// Istringstream usage
Istringstream istring;
String SS ("SS 8346520 ");
Istring. STR (SS );
Int I = 0;
String S;
Istring> S> I;
Cout <S <"" <I <Endl;
Or
Istringstream istring ("ss8346520 ");
Int I = 0;
String S;
Istring> S> I;
Cout <S <"" <I <Endl;
S content is SS, And I content is 8346520;
// Ostringstream usage
String S = "test ";
Int I = 8346520;
Int J = 0;
String S1;
Ostringstream ostring; // cannot be written as ostringstream ostring <S <"" <I;
Ostring <S <"" <I;
Cout <ostring. STR () <Endl; // The content stored in the ostring stream is test 8346520.
Istringstream istring (ostring. STR ());
Istring> S1> J; // note the sequence here;
Cout <S1 <"--" <j <Endl;
Strstream:
Array-based classes include istrstream, ostrstream, and strstream. They are used to create inputs, outputs, and inputs respectively.
Inbound/outbound streams. One of the base classes of these classes is strstreambuf, which defines several underlying attributes used by the derived classes
.
In addition to strstreambuf, istream is also the basis class of istrstream. Class ostrstream includes class ostream.
Strstream also includes iostream classes. Therefore, all array-based classes access the same
Member function.
Create an array-based output stream
To associate an output stream with an array, you can use the following ostream constructor:
Ostrstream ostr (char * Buf, int size, int mode = IOs: Out );
The Buf is a pointer to an array, which receives the characters written to the stream. The length of the array is determined by the size parameter. Default
The stream is opened in the output mode, but several items can be combined or combined as needed (for example, it can include IOs ::
APP adds the output to the end of the existing information in the array ). The default value of mode can meet most requirements.
Once an array-based output stream is opened, all outputs to the stream are placed in the array. However, any loss
Output cannot be written outside the limit of the array. Any such attempt will lead to errors.
The following is a simple program that introduces an array-based output stream.
# Include <iostream>
# Include <strstream>
Using namespace STD;
Int main ()
{
Int arraysize = 50;
Char * pbuffer = new char [arraysize];
Ostrstream ostr (pbuffer, arraysize, IOS: Out );
Ostr <"hello" <"";
Ostr <99-14 Ostr. SETF (IOs: showbase );
Ostr <100 <ends; // when outputting data to a stream object using ostrstream, use ends to end the string
Cout <pbuffer;
Delete [] pbuffer;
Return 0;
}
Input Using Arrays:
To associate an input stream with an array, use the following istrstream constructor:
Istrstream istr (char * BUF );
Buf is a pointer to an array, which serves as the character source for each input to the stream. The array specified by BUF must be empty.
End. The null Terminator is never read from the array.
The following is an example of string input:
# Include <iostream>
# Include <strstream>
Using namespace STD;
Int main ()
{
Const char s [] = "10 Hello 15 12.23 done ";
Istrstream ins (s );
Int I;
Char STR [80];
Float F;
// Reading: 10 hello
INS> I; ins> STR;
Cout <I <"" <STR <Endl;
// Reading: f12.23 done.
INS> I;
INS> F;
INS> STR;
Cout Return 0;
}
The last is the file I/O Stream:
// Ifstream usage must contain the fstream header file
Ifstream infile ("in.txt"); // or ifstream infile; infile. Open ("1.txt ");
Infile. Close (); // The fstream object can only be associated with one file. Therefore, to open another object, you must close the previous object;
Infile. Clear (); // If the fstream is created cyclically, The. Close () and clear () functions should pay attention to it;
Infile. Open ("D:/in1.txt"); // The folder address should use a backslash;
If (infile) {// check the status before use is a good habit
String S;
Infile> S; // 1st words in the file will be read; Getline (infile, S); 1st lines in the file will be read
Cout <S <Endl;
}
// Usage of ofstream
Many file modes are involved here, as follows:
In--open the file for read Operations
Out---open the file for write operations
App--add mode: Find the end Of the file before each write
Ate---after opening the file, immediately locate the file at the end of the file
Trunc---Clear the existing file stream when opening the file
Binary-perform Io operations in binary mode
Out, trunc, and app can only be used for files associated with the ofstream and fstream objects;
In mode, only files associated with ifstream and fstream objects are used.
All files can be opened in ATE or binary mode;
By default, files associated with the ifsteam object are opened in mode. files associated with the ofstream object are opened in out mode, and files opened in out mode are cleared;
String file = "D:/out.txt ";
Ofstream OUTFILE (file. c_str (); // default mode
OUTFILE <"Hello world \ n ";
OUTFILE. Close (); // pay attention to close;
OUTFILE. Clear ();
OUTFILE. Open (file. c_str (), ofstream: APP); // explicitly change the file mode and add Mode
OUTFILE <"\ nhello world again! ";