检测WordPress主题的网站,简洁大气企业网站源码,长春网站开发培训价格,有哪些做汽配的网站文章目录 什么是 I/O 流#xff1f;C I/O 流的基本类型常用的 I/O 操作1. 标准输入输出2. 文件输入输出3. 字符串流 什么是 I/O 流#xff1f;
在 C 中#xff0c;I/O 流是数据的输入和输出通道。流的本质是一个字节序列#xff0c;提供了抽象的方式来读写数据。C 使用流对… 文章目录 什么是 I/O 流C I/O 流的基本类型常用的 I/O 操作1. 标准输入输出2. 文件输入输出3. 字符串流 什么是 I/O 流
在 C 中I/O 流是数据的输入和输出通道。流的本质是一个字节序列提供了抽象的方式来读写数据。C 使用流对象来进行 I/O 操作主要分为输入流和输出流。
输入流用于从外部设备如键盘、文件等读取数据。常用的输入流对象是 std::cin。输出流用于向外部设备写入数据。常用的输出流对象是 std::cout。
C I/O 流的基本类型
C 标准库定义了多种 I/O 流类型最常用的包括 iostream包含了标准输入输出流的定义支持字符的输入输出操作。 #include iostreamfstream用于文件输入输出的流类允许程序读取和写入文件。 #include fstreamstringstream用于在内存中操作字符串的流适合在字符串与其他数据类型之间转换。 #include sstream常用的 I/O 操作
1. 标准输入输出
使用 std::cin 和 std::cout 进行简单的输入输出操作
#include iostreamint main() {int number;std::cout Enter a number: ;std::cin number;std::cout You entered: number std::endl;return 0;
}2. 文件输入输出
使用 fstream 进行文件的读写操作
#include fstream
#include iostreamint main() {std::ofstream outFile(example.txt);outFile Hello, file! std::endl;outFile.close();std::ifstream inFile(example.txt);std::string line;while (std::getline(inFile, line)) {std::cout line std::endl;}inFile.close();return 0;
}3. 字符串流
使用 stringstream 进行字符串与其他数据类型之间的转换
#include sstream
#include iostreamint main() {std::string str 123;int number;std::stringstream ss(str);ss number;std::cout Converted number: number std::endl;return 0;
}