【C++】输入输出流

C++ 提供了多种输入输出流,用于在不同的场景中处理数据的输入和输出操作。这些流主要包括标准输入输出流、文件流、字符串流、宽字符流以及自定义流等。下面是对这些输入输出流的详细介绍:

1. 标准输入输出流

标准输出流 (std::cout)

std::cout 是 C++ 标准库中的标准输出流,主要用于将程序的正常输出信息打印到控制台或终端。

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
标准错误流 (std::cerr)

std::cerr 是标准错误输出流,用于输出错误信息或警告信息。它是不带缓存的,输出信息会立即显示。

#include <iostream>

int main() {
    std::cerr << "An error occurred" << std::endl;
    return 0;
}
标准日志流 (std::clog)

std::clog 是标准日志流,通常用于输出日志信息。与 std::cerr 不同,std::clog 是带缓存的。

#include <iostream>

int main() {
    std::clog << "This is a log message" << std::endl;
    return 0;
}
标准输入流 (std::cin)

std::cin 是 C++ 标准库中的标准输入流,主要用于从控制台或终端读取输入数据。

#include <iostream>

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;
    std::cout << "You entered: " << number << std::endl;
    return 0;
}

2. 文件流

文件输出流 (std::ofstream)

std::ofstream 是文件输出流,用于将数据写入文件。

#include <fstream>
#include <iostream>

int main() {
    std::ofstream outfile("example.txt");
    if (outfile.is_open()) {
        outfile << "Writing this to a file." << std::endl;
        outfile.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }
    return 0;
}
文件输入流 (std::ifstream)

std::ifstream 是文件输入流,用于从文件读取数据。

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

int main() {
    std::ifstream infile("example.txt");
    if (infile.is_open()) {
        std::string line;
        while (getline(infile, line)) {
            std::cout << line << std::endl;
        }
        infile.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }
    return 0;
}
文件输入输出流 (std::fstream)

std::fstream 是文件输入输出流,支持同时进行读写操作。

#include <fstream>
#include <iostream>

int main() {
    std::fstream file("example.txt", std::ios::in | std::ios::out | std::ios::app);
    if (file.is_open()) {
        file << "Appending this to the file." << std::endl;
        file.seekg(0); // 将文件指针移到文件开头
        std::string line;
        while (getline(file, line)) {
            std::cout << line << std::endl;
        }
        file.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }
    return 0;
}

3. 字符串流

字符串输出流 (std::ostringstream)

std::ostringstream 是字符串输出流,用于将数据输出到一个字符串中。

#include <sstream>
#include <iostream>

int main() {
    std::ostringstream oss;
    oss << "Hello, " << "World!" << std::endl;
    std::string result = oss.str();
    std::cout << result;
    return 0;
}
字符串输入流 (std::istringstream)

std::istringstream 是字符串输入流,用于从字符串中读取数据。

#include <sstream>
#include <iostream>

int main() {
    std::string data = "123 456 789";
    std::istringstream iss(data);
    int a, b, c;
    iss >> a >> b >> c;
    std::cout << a << " " << b << " " << c << std::endl;
    return 0;
}
字符串输入输出流 (std::stringstream)

std::stringstream 是字符串输入输出流,支持同时进行字符串的读写操作。

#include <sstream>
#include <iostream>

int main() {
    std::stringstream ss;
    ss << "Hello, ";
    ss << "World!";
    std::string result = ss.str();
    std::cout << result << std::endl;

    ss.str("123 456 789");
    int a, b, c;
    ss >> a >> b >> c;
    std::cout << a << " " << b << " " << c << std::endl;
    return 0;
}

4. 宽字符流

宽字符标准输出流 (std::wcout)

用于输出宽字符(如 Unicode 字符)的标准输出流。

#include <iostream>

int main() {
    std::wcout << L"Hello, 世界!" << std::endl;
    return 0;
}
宽字符标准错误流 (std::wcerr)

用于输出宽字符的标准错误流。

#include <iostream>

int main() {
    std::wcerr << L"An error occurred" << std::endl;
    return 0;
}
宽字符标准日志流 (std::wclog)

用于输出宽字符的标准日志流。

#include <iostream>

int main() {
    std::wclog << L"This is a log message" << std::endl;
    return 0;
}
宽字符文件流 (std::wofstream, std::wifstream, std::wfstream)

用于宽字符文件的读写操作。

#include <fstream>
#include <iostream>

int main() {
    std::wofstream woutfile("example.txt");
    if (woutfile.is_open()) {
        woutfile << L"Writing this to a file." << std::endl;
        woutfile.close();
    } else {
        std::wcerr << L"Unable to open file" << std::endl;
    }
    return 0;
}

5. 自定义流

通过继承 std::streambufstd::ostream 类,可以创建自定义流以实现特殊需求的输入输出功能。

#include <iostream>
#include <streambuf>
#include <ostream>

class MyStreamBuf : public std::streambuf {
protected:
    virtual int overflow(int c) override {
        if (c != EOF) {
            c = std::toupper(c);
            if (putchar(c) == EOF) {
                return EOF;
            }
        }
        return c;
    }
};

int main() {
    MyStreamBuf myBuf;
    std::ostream myStream(&myBuf);
    myStream << "hello, world!" << std::endl;  // 输出 "HELLO, WORLD!"
    return 0;
}

总结

  • 标准输入输出流:包括 std::coutstd::cerrstd::clogstd::cin,用于控制台或终端的输入输出操作。
  • 文件流:包括 std::ofstreamstd::ifstreamstd::fstream,用于文件的读写操作。
  • 字符串流:包括 std::ostringstreamstd::istringstreamstd::stringstream,用于字符串的读写操作。
  • 宽字符流:包括 std::wcoutstd::wcerrstd::wclogstd::wofstreamstd::wifstreamstd::wfstream,用于宽字符的输入输出操作。
  • 自定义流:通过继承和重载实现特殊需求的输入输出流。

通过选择合适的输入输出流,可以满足不同的编程需求,增强程序的灵活性和可维护性。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值