日志23
一、标准输入输出流
标准输入输出流是C++程序中与用户交互的主要方式。C++标准库提供了几个预定义的流对象,用于标准输入输出。
标准输出流(std::cout)
概述:标准输出流(std::cout)是C++标准库中的标准输出流,主要用于将程序的正常输出信息打印到控制台或终端。
用法:
#include<iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
操作符:<<
被称为插入操作符,用于将数据插入到输出流中。std::endl
是C++标准库中的一个操纵符,用于在输出流中插入一个换行符,并刷新输出缓冲区。
格式化输出:C++标准库提供了多种操纵符用于格式化输出,例如 std::setw
用于设置字段宽度,std::setprecision
用于设置小数精度等。
#include <iostream> #include <iomanip> using namespace std; int main() { double pi = 3.141592653589793; cout << "Pi: " << pi << endl; cout << "Pi with 2 decimal places: " << fixed << setprecision(2) << pi << endl; cout << "Pi with 6 decimal places: " << setprecision(6) << pi << 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; }
操作符:>> 被称为提取操作符,用于从输入流中读取数据。
标准错误输出流(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; }
二、文件操作与文件流
文件操作允许程序将数据保存到磁盘上的文件中,或从文件中读取数据。C++通过文件流类(ifstream、ofstream、fstream)进行文件的读写。
文件输出流(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; }
文件打开模式
ios::in:以读模式打开文件。
ios::out:以写模式打开文件。
ios::app:追加模式,从文件末尾写入。
ios::ate:打开文件时将指针指向文件末尾。
ios::binary:以二进制模式打开文件。
ios::trunc:如果文件已存在则先清空该文件,若文件不存在则创建文件。
三、字符串流
字符串流是一种特殊的流,它允许使用与标准输入输出流相同的操作符来处理字符串数据。C++标准库提供了两种类型的字符串流:std::istringstream(从字符串中读取数据)和std::ostringstream(将数据输出到一个字符串中)。此外,std::stringstream支持同时进行字符串的读写操作。
字符串输出流(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; }