C + + learning notes 7

Keywords: C++

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:

  1. 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:

signeffect
ios::fixedNot output according to scientific counting method
ios::scientifucOutput according to scientific counting method
ios::showpointFor floating point types, the decimal point is displayed
ios::showposA positive number is preceded by a positive sign
ios::rightAchieve neat output
ios::leftAchieve neat output
ios::decIntegers are output in decimal
ios::octIntegers are output in octal
ios::hexIntegers are output in hexadecimal format
ios::uppercaseFor floating-point types, count according to science
ios::showbaseDisplay 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

functiondescribeCorresponding operator
self()set marksetiosflags()
uself()Unset flagresetiosflags()
self(0,ios::floatfield)Restore default settings for tagsnothing
precision(int)Sets the precision of floating point number outputsetprecision(int)
width(int)Sets the digital width of the output, which is only valid for the next outputsetw(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:

Posted by dark_destroyer on Fri, 15 Oct 2021 23:55:21 -0700