C++ programming [7] input/output stream

Thanks to the content provider: Wudi Software Development Studio in Jinniu District
Continued from the previous article: C++ Programming [6] Polymorphism and Virtual Functions

Chapter 7: Input/Output Stream

1. Introduction to Streaming

Insert picture description here
Insert picture description here
Insert picture description here

Second, the standard stream object

Insert picture description here

#include<iostream>

using namespace std;

int main() {
    
    
    int x, y;
    cin >> x >> y;
    freopen("test.txt", "w", stdout);   // 将标准输出重定向到文件test.txt
    if (y == 0) {
    
       // 除数为0则输出错误信息
        cerr << "error." << endl;
    } else {
    
    
        cout << x << "/" << y << "=" << x / y << endl;
    }
    return 0;
}
#include<iostream>

using namespace std;

int main() {
    
    
    int x, count, sum = 0;
    freopen("input.txt", "r", stdin);   // 将标准输入重定向到文件input.txt
    for (count = 0; count < 10; count++) {
    
      // 从输入流中读入10个整数进行处理
        cin >> x;
        if (cin.eof()) {
    
      // 到了文件的结尾的话  
            break;	// 若输入流已经结束则退出
        } else {
    
    
            sum += x;
        }
    }
    cout << "前10个整数的平均值= " << 1.0 * sum / 10 << endl;
    return 0;
}

Three, control I/O format

Insert picture description here

1. Stream Manipulator

Insert picture description here
Insert picture description here

#include<iostream>
#include<iomanip>

using namespace std;

int main() {
    
    
    int n = 65535, m = 20;
    // 1。分别输出一个整数的十进制、十六进制和八进制表示
    cout << "1)" << n << "=" << hex << n << "=" << oct << n << endl;
    // 2.使用setbase分别输出一个整数的十进制、十六进制和八进制表示
    cout << "2)" << setbase(10) << m << "=" << setbase(16) << m << "=" << setbase(8) << m << endl;
    // 3.使用showbase和setbase分别输出一个整数的十进制、十六进制和八进制表示
    cout << "3)" << showbase;   // 输出表示数值进制的前缀
    cout << setbase(10) << m << "=" << setbase(16) << m << "=" << setbase(8) << m << endl;
    return 0;
}
#include<iostream>
#include<iomanip>

using namespace std;

int main() {
    
    
    double x = 12.34;
    cout << "1." << setiosflags(ios::scientific | ios::showpos) << x << endl;
    cout << "2." << setiosflags(ios::fixed) << x << endl;
    cout << "3." << resetiosflags(ios::fixed) << setiosflags(ios::scientific | ios::showpos) << x << endl;
    cout << "4." << resetiosflags(ios::showpos) << x << endl;   // 清楚要输出正号的标志
    return 0;
}

2. Logo word

Insert picture description here

Four, call the member function of cout

Insert picture description here

#include<iostream>

using namespace std;

int main() {
    
    
    double values[] = {
    
    1.23, 20.3456, 300.4567, 4000.45678, 50000.1234567};
    cout.fill('*'); // 设置填充字符为星号*
    for (int i = 0; i < sizeof(values) / sizeof(double); i++) {
    
    
        cout << "values[" << i << "]=(";
        cout.width(10); // 设置输出宽度
        cout << values[i] << ")" << endl;
    }
    cout.fill(' '); // 设置填充字符为空格
    int j;
    for (j = 0; j < sizeof(values) / sizeof(double); j++) {
    
    
        cout << "values[" << j << "]=(";
        cout.width(10); // 设置输出宽度
        cout.precision(j + 3);  // 设置保留有效数字
        cout << values[j] << ")" << endl;
    }
    return 0;
}
#include<iostream>

using namespace std;

int main() {
    
    
    char str[30] = "0123456789";
    cout.write(str, 5); // 将str的前5个字节写入到输出流中
    return 0;
}

Five, call the member function of cin

Insert picture description here

1.get() function

Insert picture description here

2.getline() function

Insert picture description here

#include<iostream>
using namespace std;

int main() {
    
    
    char buf[10];
    int i = 0;
    // 若输入流的一行超过9个字符,则后面的会被抛弃且退出循环
    while(cin.getline(buf, 10)) {
    
    
        cout << ++i << ":" << buf << endl;
    }
    cout << "last:" << buf << endl;
    return 0;
}

3.eof() function

Insert picture description here

4.ignore() function

Insert picture description here

#include<iostream>

using namespace std;

int main() {
    
    
    char str[30];
    while(!cin.eof()) {
    
    
        cin.ignore(10, ':');
        if (!cin.eof()) {
    
    
            cin >> str;
            cout << str << endl;
        }
    }
    return 0;
}

5.peek() function

Insert picture description here

Next: C++ Programming [8] File Operation

Guess you like

Origin blog.csdn.net/weixin_43606158/article/details/114240364