C++中IOstream解析

发布于:2025-05-23 ⋅ 阅读:(11) ⋅ 点赞:(0)

1-概念

<iostream> 是C++编程中处理标准输入(stdin)、标准输出(stdout)和标准错误(stderr)的主要工具。通过 <iostream>,开发者可以方便地进行格式化输入输出操作,支持各种数据类型,并且具有高度的灵活性和可扩展性。

1.1 头文件包含

#include<iostream>

1.2 命名空间

<iostream> 中的类和函数位于 std 命名空间内,通常需要使用 std:: 前缀或使用 using 声明来简化代码。

1.3 标准流对象

  • std::cin:标准输入流,默认连接到键盘输入。
  • std::cout:标准输出流,默认连接到终端显示。
  • std::cerr:标准错误流,默认连接到终端显示,用于输出错误信息。
  • std::clog:标准日志流,默认连接到终端显示,用于输出日志信息。

2. 常用类与对象

2.1 std::istream

输入流类,提供了从输入源读取数据的方法。std::cin 是 std::istream 的一个实例。

2.2 std::ostream

输出流类,提供了向输出目标写入数据的方法。std::coutstd::cerr 和 std::clog 都是 std::ostream 的实例。

2.3 std::iostream

输入输出流类,继承自 std::istream 和 std::ostream,提供了双向的输入输出功能。

2.4 std::ifstream 和 std::ofstream

文件输入流和文件输出流,用于从文件读取数据和向文件写入数据。

2.5 std::fstream

文件输入输出流,结合了 std::ifstream 和 std::ofstream 的功能。

3. 基本输入输出操作

3.1 输出操作

使用 << 操作符将数据写入输出流。

 例子:

#include <iostream>

int main() {
    int a = 10;
    double b = 3.14;
    std::string str = "just a test !";
    
    std::cout << "Integer: " << a << std::endl;
    std::cout << "Double: " << b << std::endl;
    std::cout << "String: " << str << std::endl;
    
    return 0;
}

 

3.2 输入操作

使用 >> 操作符从输入流中读取数据。

例子:

#include <iostream>

int main() {
    int n;
    std::string test1;
    
    std::cout << "Enter a test  ";
    std::cin >> name;
    
    std::cout << "out a test ";
    std::cin >> age;
    
    std::cout << "test1: " << test1 << ", n: " << n<< std::endl;

    return 0;
}

3.3 格式化输出

<iostream> 提供了多种格式化选项,如设置宽度、对齐方式、填充字符等。

例子:
 

#include <iostream>
#include <iomanip>

int main() {
    double pi = 3.141592653589793;
    
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "Pi: " << pi << std::endl;
    
    std::cout << std::setw(10) << "Left" << std::endl;
    std::cout << std::setw(10) << std::left << "Left" << std::endl;
    std::cout << std::setw(10) << std::right << "Right" << std::endl;
    
    return 0;
}

3.4 错误处理

输入输出操作可能会失败,可以通过检查流的状态来检测错误。

例子:
 

#include <iostream>

int main() {
    int num;
    std::cout << "Enter a number: ";
    std::cin >> num;
    
    if(std::cin.fail()) {
        std::cerr << "Invalid input!" << std::endl;
        std::cin.clear(); // 清除错误标志
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略错误输入
        return 1;
    }
    
    std::cout << "You entered: " << num << std::endl;
    return 0;
}

4. 别的用法

4.1 用户自定义类型的输入输出

可以通过重载 << 和 >> 操作符,实现用户自定义类型的输入输出。

#include <iostream>

class Point {
public:
    int x, y;
    
    // 重载输出操作符
    friend std::ostream& operator<<(std::ostream& os, const Point& p) {
        os << "(" << p.x << ", " << p.y << ")";
        return os;
    }
    
    // 重载输入操作符
    friend std::istream& operator>>(std::istream& is, Point& p) {
        is >> p.x >> p.y;
        return is;
    }
};

int main() {
    Point p;
    std::cout << "Enter point coordinates: ";
    std::cin >> p;
    std::cout << "Point: " << p << std::endl;
    return 0;
}

4.2 文件输入输出

使用 std::ifstream 和 std::ofstream 可以进行文件读写操作。

例子:

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ofstream outfile("example.txt");
    if(!outfile) {
        std::cerr << "Failed to open file for writing." << std::endl;
        return 1;
    }
    
    outfile << "Hello, File!" << std::endl;
    outfile.close();
    
    std::ifstream infile("example.txt");
    if(!infile) {
        std::cerr << "Failed to open file for reading." << std::endl;
        return 1;
    }
    
    std::string line;
    while(std::getline(infile, line)) {
        std::cout << line << std::endl;
    }
    infile.close();
    
    return 0;
}

4.3 字符串流

使用 std::stringstream 可以将字符串作为输入输出流进行处理。

例子:
 

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string data = "1 2 3 4 5";
    std::stringstream ss(data);
    int num;
    int sum = 0;
    
    while(ss >> num) {
        sum += num;
    }
    
    std::cout << "Sum: " << sum << std::endl;//输出sum=15
    return 0;
}

4.4 缓冲控制

可以通过 std::flush 和 std::endl 控制输出流的缓冲行为。

#include <iostream>

int main() {
    std::cout << "Hello, ";
    std::cout << "World!" << std::endl; // 输出并换行,并刷新缓冲区
    std::cout << "Immediate " << std::flush; // 输出并立即刷新缓冲区
    std::cout << "Flush" << std::endl;
    return 0;
}

至此结束,如果有错误还请指正。


网站公告

今日签到

点亮在社区的每一天
去签到