Stream and file I/O operations:
Input flow: the flow direction of data is to enter the program;
Output stream: the flow direction of data is to leave the program;
File I/O:
To write data to a file or read data from a file, you need to use ofstream and i fstream in the fsstream library;
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream st_1; ofstream st_2; int a,b; string c,d; st_1.open("D:\\C++_new\\c++_1\\test.txt"); st_1 >> c >> d; cout << c << "\t" << d << endl; st_2.open("D:\\C++_new\\c++_1\\test_1.txt"); st_2 << "Display:" << c << "\t" << d << endl; return 0; }
Trap: restrictions on the use of rheological quantities:
- The assignment expression cannot be used for the assignment of flow variables. The object of flow type should be used as the parameter:
Add content to existing files:
It is mainly to add appropriate parameters when associating with files, as follows:
ofstream out; out.open("test.txt",ios::app);
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream st_1; ofstream st_2; int a,b; string c,d; st_1.open("D:\\C++_new\\c++_1\\test.txt"); st_1 >> c >> d; cout << c << "\t" << d << endl; st_2.open("D:\\C++_new\\c++_1\\test_1.txt",ios::app); st_2 <<endl << "Redisplay:" << c << "\t" << d << endl; return 0; }
Another way to open a file:
ifstream st1; st1.open("test.txt"); //Equivalent to ifstream st1("test.txt"); //**********************************************// ofstream st2; st2.open("test.txt"); //Equivalent to ofstream st2("test.txt");
File association success:
If the association is unsuccessful, st1.fail() will return true;
ifstream st1; st1.open("test.txt"); st1.fail();
2 is returned for successful reading, and 1 is returned for failure;
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream st_1; string c; st_1.open("D:\\C++_new\\c++_1\\test.txt"); if (st_1.fail()) { cout << "1"; } else { cout << "2"; } return 0; }
Judge whether the file has reached the end: eof();
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream st_1; char a; st_1.open("D:\\C++_new\\c++_1\\test.txt"); st_1.get(a); while(!st_1.eof()) { cout << a; st_1.get(a); } return 0; }
Another end method:
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream st_1; char a; int sum; st_1.open("D:\\C++_new\\c++_1\\test.txt"); while(st_1 >> a) { cout << a; sum++; } cout << sum; return 0; }
The open() function cannot use string internally, only c string can be used, but c can be used_ Str() function conversion.
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream st_1; string a,b,c; cin >> a; b = "D:\\C++_new\\c++_1\\" + a; st_1.open(b.c_str()); st_1 >> c; cout << c; return 0; }
Format output:
sign | effect |
---|---|
ios::fixed | Not output according to scientific counting method |
ios::scientifuc | Output according to scientific counting method |
ios::showpoint | For floating point types, the decimal point is displayed |
ios::showpos | A positive number is preceded by a positive sign |
ios::right | Achieve neat output |
ios::left | Achieve neat output |
ios::dec | Integers are output in decimal |
ios::oct | Integers are output in octal |
ios::hex | Integers are output in hexadecimal format |
ios::uppercase | For floating-point types, count according to science |
ios::showbase | Display output decimal (0 before octal, 0x before hexadecimal) |
The above form can be used through self();
It can also be ended with unself();
Controller:
endl/setw/setprecisionm
function | describe | Corresponding operator |
---|---|---|
self() | set mark | setiosflags() |
uself() | Unset flag | resetiosflags() |
self(0,ios::floatfield) | Restore default settings for tags | nothing |
precision(int) | Sets the precision of floating point number output | setprecision(int) |
width(int) | Sets the digital width of the output, which is only valid for the next output | setw(int) |
fill(char) | When the width of the output field is greater than the width of the output, specify the characters filled in the insufficient bits; It is blank by default; | setfill() |
recursion
When a function contains a call to itself, it is called recursion
#include <iostream> using namespace std; void test(int a); int main() { int a; cin >> a; test(a); return 0; } void test(int a) { if (a/10) { test(a/10); cout << a%10<<endl; } else { cout << a%10 << endl; } }
Recursion with return value: (compute n!)
#include <iostream> using namespace std; int test(int a); int main() { int a,b; cin >> a; b = test(a); cout << a << "!:" << b; return 0; } int test(int a) { if (a!=0) { return a * test(a - 1); } else { return 1; } }
Binary search: