Fourth chapter input Output stream
Strfiile.cpp
/*** book: "thinkinginc++" * function: input/output stream of a file * time: October 12, 2014 15:15:59* Author: cutter_point*/#include <iostream>//on many platforms <fstream> will automatically include <iostream> but not all # include <fstream> #include ". /require.h "using namespace Std;int Main () {const int sz=100; Size of buffer pool char Buf[sz]; Buffer {ifstream in ("Strfile.cpp");//read in file assure (in, "Strfile.cpp"); Determine that the file was opened Ofstream out ("Strfile.txt"); Output file assure (out, "Strfile.txt"); int i=1; Represents the first row while (In.get (buf, SZ))//reads in SZ-1 characters or encounters ' \ n ' {in.get (); Remove ' \ n ' because the above is not read ' \ n ', here ' \ n ' read in, but do not handle cout<<buf<<endl; Endl must be added here, otherwise it will not be wrapped out<<i++<< "\ t:" <<buf<<endl; }} Ifstream in ("Strfile.txt"); Assure (in, "Strfile.txt"); while (In.getline (buf, SZ)) {char* cp=buf; while (*CP! = ': ')//In order to remove the preceding ordinal ++cp; cp+=2; This is to remove the ":" cout<<cp<<endl; } return 0;}
4.6 Locating in the input and output stream
Flag function |
Ios::in Open the input file. Using this open mode for Ofstream can make the existing File is not truncated |
Ios::out Open the output file. |
Ios::app open an output file for append only |
Ios::ate open an existing file and point the file pointer to the end of the file Truncate old file if file exists |
Ios::trunc If the file exists, truncate the old file |
Ios::binary opens the file in binary mode. Open as text by default |
Read (unsigned char *buf,int num); write (const unsigned char *buf,int num);
Read () reads num characters from a file into the cache pointed to by buf, and if it is at the end of the file when NUM characters have not been read, you can use the member function int gcount () to get the number of characters actually read, while write () writes NUM characters from the cache pointed to by BUF to the file. It is important to note that the type of cache is unsigned char *, which may sometimes require a type conversion.
* SEEKG () to the input file location, there are two parameters:
* First: Indicates an offset, can be negative, positive means backward, negative indicates forward
* Second: The base address of the offset, which can be:
* Start of Ios::beg input stream
* Ios::cur the current position of the input stream
* End of Ios::end input stream
4.7 string input/output stream
WS refers to Std::ws, which is used to remove spaces.
Like what:
int i;float F;char buf[100];cin >> i >> f;cin >> std::ws;cin.getline (buf, 100);
At run time, you need to enter an integer number, enter a space, and then enter a floating point. You can then enter a number of spaces and finally enter a string.
Cin after reading the integer and floating-point numbers, it continues to read the string you entered and ignores several spaces between the floating-point number and the string.
Ostring.cpp
/*** book: "thinkinginc++" * Function: Output string stream * Time: October 12, 2014 15:17:02* Author: cutter_point*/#include <iostream> #include < Sstream> #include <string>using namespace Std;int main () { cout<< "Enter an int, a float, a string of strings" ; int i; float F; cin>>i>>f; cin>>ws; Remove the space, WS is the STD inside, in order to ignore the space string stuff; Getline (cin, stuff); Get a row of strings ostringstream os; String output stream os<< "integer =" <<i<<endl; os<< "float =" <<f<<endl; os<< "string =" <<stuff<<endl; String Result=os.str (); cout<<result<<endl; return 0;}
4.8 Formatting of the input stream
On/OFF flag |
Role |
Ios::skipws |
Skip Spaces |
Ios::showbase |
Indicates the cardinality of a number when printing an integer value |
Ios::showpoint |
Show floating point worthy decimal point and truncate 0 of the end of the number |
Ios::uppercase |
Use uppercase a~f when displaying hexadecimal values, showing the E in the scientific count |
Ios::showpos |
The plus sign (+) before displaying a positive number |
Ios::unitbuf |
"Cell buffers". Refresh after each insert |
Ios::basefield |
Role |
Ios::d EC |
Decimal |
Ios::hex |
Hexadecimal |
Ios::oct |
Octal |
Ios::floatfield |
Role |
Ios::scientific |
Display floating-point numbers in scientific notation |
Ios::fixed |
Display floating-point numbers in a fixed format |
"Automatic" |
The Precision field indicates the number of digits of all valid digits |
Ios::adjustfield |
Role |
Ios::left |
Value left Justified |
Ios::right |
Align Right |
Ios::internal |
Front sign left aligned, trailing value right |
Function |
Role |
Int Ios::width () |
Returns the current width. Default is 0 |
int ios::width (int n) |
Sets the width and returns the current width |
Int Ios::fill () |
Returns the current padding character, the default is a space |
int Ios::fill (int n) |
Sets the fill character and returns the preceding character |
Int iOS::p recision () |
Returns the current floating-point precision |
int iOS::p recision (int n) |
Sets the precision of the floating-point number, returning the previous precision. |
Format.cpp
/*** book: "thinkinginc++" * Function: about the use of some format domains * Time: October 12, 2014 15:17:35* Author: cutter_point*/#include <iostream> #include <fstream>using namespace std; #define D (A) t<< #A <<endl; A//d (a) represents the first execution of the latter, followed by the meaning of a delegate, int main () {Ofstream T ("Format.txt"); D (int i=47); D (float f=123456789.543235453); Const char* s= "I ' m cutter_point"; D (T.SETF (IOS::UNITBUF);)//"Cell buffer". The Radix d (T.SETF (Ios::uppercase | ios::showpos) of the number is indicated when the D (T.SETF (ios::showbase);)//print integer value is refreshed after each insert;) D (t<<i<<endl;)//default is 10 binary D (T.SETF (Ios::hex, Ios::basefield);) D (t<<i<<endl;) D (T.SETF (ios::oct, Ios::basefield);) D (t<<i<<endl;) D (T.UNSETF (ios::showbase);)//Print integer value indicating the cardinality of the number D (T.SETF (iOS::d ec, Ios::basefield);) D (T.SETF (Ios::left, Ios::adjustfield);)//value left justified D (T.fill (' 0 ');) D (t<< "Fill char:" <<t.fill () <<endl;) D (t.width);) t<<i<<endl; Right-justified D (T.SETF (ios::right, Ios::adjustfield);) D (T.WIdth (;)) t<<i<<endl; The front sign is left aligned, and the trailing value is right-justified D (T.SETF (ios::internal, Ios::adjustfield);) D (t.width);) t<<i<<endl; D (t<<i<<endl;) D (T.UNSETF (ios::showpos);) D (T.SETF (ios::showpoint);) D (t<< "prec =" <<t.precision () <<endl;)//sets the precision of the floating-point number, returning the previous precision. D (T.SETF (ios::scientific, Ios::floatfield);) D (t<<endl<<f<<endl;) D (T.UNSETF (ios::uppercase);) D (t<<endl<<f<<endl;) D (T.SETF (ios::fixed, Ios::floatfield);)//display floating-point number D (t<<f<<endl;) in a fixed format D (t.precision);) D (t<< "prec =" <<t.precision () <<endl;)//sets the precision of the floating-point number, returning the previous precision. D (t<<endl<<f<<endl;) D (T.SETF (ios::scientific, Ios::floatfield);) D (t<<endl<<f<<endl;) D (T.SETF (ios::fixed, Ios::floatfield);)//display floating-point number D (t<<f<<endl;) in a fixed format D (t.width);) t<<s<<endl; D (t.width);) t<<s<<endl; D (T.SETF (iOs::left, Ios::adjustfield);) D (t.width);) t<<s<<endl; return 0;}
4.9 Manipulation operators
Manipulation operators |
Role |
Showbase |
The cardinality (Dec, Oct, or hex) that displays numbers when output shaping number |
Noshowbase |
|
Showpos |
Show positive sign (+) in front |
Noshowpos |
|
Uppercase |
Use uppercase a~f to display hexadecimal numbers, using uppercase E in scientific counting numbers |
Nouppercase |
|
Showpoint |
Displays the decimal point and the trailing 0 of the floating-point number |
Noshowpoint |
|
Skipws |
Skip spaces in the input |
Noskipws |
|
Left |
Value left Justified |
Right |
Right-aligned, padding characters to the left |
Internal |
Place a fill character between a guide or cardinality indicator and a value |
Scientific |
Indicates the preferred output format for floating-point numbers |
Fixed |
|
4.9 operator operator 4.9.3 utility operator
Create the operator yourself.
Effector.cpp
/*** book: "thinkinginc++" * Function: Utility operator * Time: October 12, 2014 15:18:32* Author: cutter_point*/#include <cassert> #include < limits> #include <sstream> #include <string> #include <iostream>using namespace Std;class fixw{ String Str;public:fixw (const string& s, int width): str (s, 0, width) {} friend ostream& operator<< (o stream& OS, const fixw& FW) {return os<<fw.str; }};typedef unsigned long ulong; Binary output Class bin{ulong N;public:bin (ulong nn) {n=nn;} Friend ostream& operator<< (ostream& os, const bin& b) {const ULONG Ulmax=numeric_limits<ul Ong>::max (); ULONG bit=~ (ulmax>>1); while (bit) {os<< (B.N & bit?) ' 1 ': ' 0 '); bit>>=1; } return OS; }};int Main () {string words= "C + + that does us happy, make us wise"; for (int i=words.size ();------->= 0;) {Ostringstream s; Output stream s<<fixw (words, i); ASSERT (s.str () = = Words.substr (0, i)); Cout<<s.str () <<endl; } ostringstream xs, Ys; Xs<<bin (0xCAFEBABEUL); Cout<<xs.str () <<endl; Ys<<bin (0x76543210ul); Cout<<ys.str () <<endl; return 0;}
"Thinkinginc++" 72, input and output stream