Stream: several bytes of data arrive from one end to the other, which is called stream
Stream class system: stream objects, and stream operators > > <<
Input / output stream
ostream class: cout,cerr,clog, istream class cin
#include<iostream> using namespace std; void testostream() { cout << "standard output " << endl; //Can redirect cerr << "Standard error" << endl; //Cannot redirect clog << "Standard error output" << endl;//Here you can redirect to a file } int main() { return 0; }
Many people don't know much about redirection. File redirection is that the input or output of the program can be completed by files
Input from a file into a program, or into a file in a program
#include<iostream> #include<cstdio> using namespace std; void testostream() { cout << "standard output " << endl; //Can redirect cerr << "Standard error" << endl; //Cannot redirect clog << "Standard error output" << endl;//Here you can redirect to a file } int main() { int temp, temp2; freopen("fox.txt","r",stdin);//Redirect input when the program runs, read data into the file scanf("%d%d",&temp,&temp2);//Data will be fetched according to format control characters cout << temp << temp2 << endl;//We print the test freopen("Fox 2.txt", "w", stdout);//Oriented to output printf("%d", temp+ temp2);//The print here will be output to a file return 0; }
Processing of output character class
One is to call member functions, and the other is to call normally
#include<iostream> #include<cstdio> using namespace std; void testostream() { cout << "standard output " << endl; //Can redirect cerr << "Standard error" << endl; //Cannot redirect clog << "Standard error output" << endl;//Can redirect //Character class processing cout.put('A');//Call member function cout << 'A' << endl;//Normal output char c = 'W'; cout.put(c);//Variables and constants are OK cout << c << endl; cout.write("I love you\n",4);//To intercept the output, this member function needs to be limited in length //Input stream //Character input c = cin.get();//The return value of the get() function is our input cout.put(c); //String input while (cin.get() != '\n');//Clear buffer processing char str[20] = ""; cin.getline(str,20); cout.write(str,20); //Note that the processing must be processed as char * and cannot process C++ string classes //Format control } int main() { testostream(); return 0; }
Format control characters: include header file iomanip
One is to call member functions, and the other is to use flow control characters
#include<iostream> #include<cstdio> #include<iomanip> using namespace std; void testiomanip() { double pi = 3.1415926535; cout << setprecision(4);//Set stream format output.. Here is to set the number of significant digits cout << pi << endl; cout << fixed << setprecision(4);//The combination of fixed is the setting of effective decimal places cout << pi << endl; cout.precision(8);//Basically, all flow controllers correspond to a member function cout << pi << endl; cout << hex << 58 << endl;//hexadecimal cout << oct << 48 << endl;//octal number system cout << setbase(16);//Set to hexadecimal, you must enter the commonly used 8 10 16. There is no binary cout << 58 << endl; cout << setw(8);//Set interval / / the default is right alignment cout << setiosflags(ios::left);//Set to left alignment //cout << setiosflags(ios::right);// Set right alignment cout << setw(8) << 58 << setw(8) << 68 << endl; } int main() { testiomanip(); return 0; }
2. Character stream processing:
Character stream header file ssstream
The subclass is the character stream class for istreamstream input
The subclass is the stream class of ostreamstream for output
We generally use stream, which can be input or output
General character stream object for String segmentation String conversion problem
#include<iostream> #include<cstdio> #include<iomanip> #include<sstream> using namespace std; void teststringstream() { //Building character stream objects stringstream Fox(string("I am Fox"));//Construction mode stringstream Foxson; Foxson << "I love you!"; //Stream the data in cout << Foxson.str();//Read the data in the stream string data; Foxson >> data;//Stream objects into data cout << data << Foxson.str() << endl;//Does not affect the original data //Foxson.str("");// Delete data by overwrite //Conversion between string and number int Num = 34567; char input[20] = ""; stringstream tempstr(input);//Build an empty character stream object tempstr << Num;//Stream the numbers in tempstr >> input;//Stream out as a string cout << input << endl; //Data is converted according to control characters //String to number is easier stringstream tempstrnum("12345"); tempstrnum >> Num; cout << Num << endl; //String segmentation stringstream sdata("56,78,9121,84,48,1841,10"); int numdata[7]; char cdata[6]; for (int i = 0; i < 7; i++) { if (i==6)//The last time there was no comma { sdata >> numdata[i];//data is int, which streams objects 56 as integers } else { sdata >> numdata[i];//data is int, which streams objects 56 as integers sdata >> cdata[i];//The character flows the comma away //Data will be retrieved according to the type of variable } } for (int i = 0; i < 7; i++) { cout << numdata[i]<<"\t"; } cout << endl; //For multiple data conversion operations on the same stream, clear must be performed on the stream object tempstr.clear(); tempstr << Num;//Stream the numbers in tempstr >> input;//Stream out as a string cout << input << endl; //Use clear multiple times to re-enter the data } int main() { teststringstream(); return 0; }
3. File flow
ofstream class // Output output to file, write operation
ifstream Class / / read operation
fstream class / / readable and writable
Header file: fstream
Open file: read / write mode
ios::in / / open the file by reading
ios::out / / open the file by writing. It has the function of creating, but it will overwrite the file
ios::app / / append mode
ios::atr / / open the existing file. The file points to the end of the file
ios::trunc / / create function
ios::nocreate / / does not have the function of creating
ios::norlace / / do not replace
ios::binary / / binary mode
Combination mode|
Readable and writable ios::in|ios::out
Binary readable writable ios::binary|ios::in|ios::out
true is returned if the file is opened successfully, false is returned if it fails
Close the file object. close();
Read / write mode:
Read and write by stream
Note that spaces and line breaks are ignored
#include<iostream> #include<cstdio> #include<iomanip> #include<sstream> #include<fstream> using namespace std; void asciiRWFile(const char *ReadFlieName,const char *WriteFlieName) { fstream read(ReadFlieName,ios::in); fstream writ(WriteFlieName, ios::out); //1. Read and write by stream //while (1) //eof returns false if it does not end, so it is reversed //{ // char key; // read >> key; // if (read.eof())//eof returns true at the end, so execute // { // break; // } // writ << key;// It can be saved directly to the file pointed to by the file object //} //2. The stream ignores spaces and line breaks. We read and write through member function characters //while (1) //{ // char key; // read.get(key); // if (read.eof())//eof returns true at the end, so execute // { // break; // } // writ.put(key);// Output to the specified file //} //Stream string read / write mode while (!read.eof()) { char str[1024] = "";//Initialization is required for each read read.getline(str,1024); writ.write(str,strlen(str));//Count the visible length and write it without line feed writ << endl;//Flow a new line in } read.close(); writ.close(); } int main() { asciiRWFile("Read file.txt","123.txt"); return 0; }
Binary read / write:
Binary reading and writing is much simpler than character reading and writing
#include<iostream> #include<cstdio> #include<iomanip> #include<sstream> #include<fstream> using namespace std; void binaryRWFile(const char* ReadFlieName, const char* WriteFlieName) { fstream read(ReadFlieName, ios::in|ios::binary); fstream writ(WriteFlieName, ios::out|ios::binary); while (!read.eof()) { char str[1024] = ""; read.fstream::read(str, 1024);//There is a problem with the same name. Limit the class name writ.write(str, 1024); } read.close(); writ.close(); } int main() { binaryRWFile("Read file.txt","21323.txt"); return 0; }
File pointer positioning
ios::beg;// File start
ios::end;// end of file
ios::cur / / current location of the file
#include<iostream> #include<cstdio> #include<iomanip> #include<sstream> #include<fstream> using namespace std; void testSeekRead(const char* ReadFlieName) { fstream fread(ReadFlieName, ios::in); fread.seekg(4); char key = fread.get(); cout << key << endl; fread.seekg(-4, ios::end); key = fread.get(); cout << key << endl; } int main() { testSeekRead("Read file.txt"); return 0; }