网站开发多少钱农民网站的类型有哪几种
C++标准IO流
- 使用cout进行标准输出,即数据从内存流向控制台(显示器)
 - 使用cin进行标准输入,即数据通过键盘输入到程序中
 - 使用cerr进行标准错误的输出
 - 使用clog进行日志的输出
 
C++文件IO流
文件流对象
ofstream:只写
ofstream 是 C++ 中用于输出文件操作的一个类,它可以创建新文件用于写入或者向已存在的文件写入数据。ofstream 属于 <fstream> 头文件中定义的一部分,是用于处理文件输出流的。
基本用法
1.包含头文件:要使用 ofstream,首先需要包含 <fstream> 头文件。
   #include <fstream>
 
 
2.创建对象:然后创建一个 ofstream 类的对象,并指定要操作的文件。
   std::ofstream outFile("example.txt");
 
 
上面的代码创建了一个 ofstream 对象名为 outFile,并将其与名为 "example.txt" 的文件关联起来。如果文件不存在,它会被创建。
3.写入文件:使用 << 运算符向文件写入数据。
cpp
   outFile << "Hello, world!" << std::endl;
 
 
4.关闭文件:完成文件写入后,应该关闭文件。虽然文件通常会在 ofstream 对象被销毁时自动关闭,但显式关闭是一个好习惯。
   outFile.close();
 
 
高级特性
- 打开模式:在打开文件时,可以指定不同的模式,比如:
std::ios::app(追加模式),std::ios::binary(二进制模式)等。 
   std::ofstream outFile("example.txt", std::ios::app);
 
 
- 检查文件是否成功打开:可以使用 
.is_open()方法来检查文件是否成功打开。 
   if (outFile.is_open()) {// 文件成功打开} else {// 打开文件失败}
 
 
- 检查写入是否成功:就像标准输出流一样,可以检查 
ofstream对象的状态,以确定数据是否成功写入。 
   if (outFile << "Hello, world!") {// 写入成功} else {// 写入失败} 
ifstream:只读
ifstream 是 C++ 标准库中用于从文件读取数据的一个类,是输入文件流类。它定义在 <fstream> 头文件中,用于处理文件的读操作。
基本用法
1.包含头文件:使用 ifstream 需要包含头文件 <fstream>。
   #include <fstream>
 
 
2.创建对象:创建一个 ifstream 类的对象,并与要读取的文件关联。
   std::ifstream inFile("example.txt");
 
 
如果文件 "example.txt" 存在,inFile 会打开它以进行读取。
3.读取文件:使用 >> 运算符或 getline 函数从文件读取数据。
   std::string data;while (inFile >> data) {// 处理 data}std::string line;while (std::getline(inFile, line)) {// 处理每行 line}
 
 
4.关闭文件:读取完文件后,应该关闭文件。虽然文件通常会在 ifstream 对象被销毁时自动关闭,但显式关闭是一个好习惯。
   inFile.close();
 
 
高级特性
- 打开模式:在打开文件时,也可以指定不同的模式,如 
std::ios::binary(二进制模式)。 
   std::ifstream inFile("example.bin", std::ios::binary);
 
 
- 检查文件是否打开成功:使用 
.is_open()方法来检查文件是否成功打开。 
   if (inFile.is_open()) {// 文件成功打开} else {// 打开文件失败}
 
 
- 检查读取状态:可以检查 
ifstream对象的状态,确定读取操作是否成功。 
   if (inFile >> data) {// 读取成功} else {// 读取失败或文件结束} 
fstream:读+写
fstream 是 C++ 中处理文件输入输出操作的一个类,属于标准库中的 <fstream> 头文件。fstream 类综合了 ifstream(输入文件流)和 ofstream(输出文件流)的功能,允许同时对文件进行读写操作。
基本用法
1.包含头文件:首先,要使用 fstream,需要包括头文件 <fstream>。
   #include <fstream>
 
 
2.创建对象:可以创建一个 fstream 类的对象,并根据需要打开文件进行读取、写入或两者兼顾。
   std::fstream file("example.txt");
 
 
这会尝试打开 "example.txt" 文件。
3.指定模式打开文件:在创建 fstream 对象时,可以通过第二个参数指定文件打开的模式,例如仅读、仅写、读写等。
   std::fstream file("example.txt", std::ios::in | std::ios::out);
 
 
上述代码中,std::ios::in 指定了文件为读取模式,std::ios::out 指定了文件为写入模式。
4.读写操作:使用 fstream 对象,你可以读写文件就像使用 ifstream 或 ofstream 一样。
   // 写入file << "Hello, fstream!" << std::endl;// 读取std::string line;getline(file, line);
 
 
5.关闭文件:完成文件操作后,应该关闭文件。虽然文件会在 fstream 对象被销毁时自动关闭,但显式关闭文件是一个好习惯。
   file.close();
 
 
高级特性
-  
打开文件的多种模式:除了
std::ios::in和std::ios::out,还有其他模式如std::ios::app(追加模式),std::ios::binary(二进制模式)等,可以根据需要进行组合。 -  
检查文件是否打开成功:使用
.is_open()方法检查文件是否成功打开。 
   if (file.is_open()) {// 文件成功打开} else {// 打开文件失败}
 
 
- 文件定位操作:
fstream提供了seekg和seekp函数,用于移动读取和写入位置指针,实现随机访问文件内容。 
文件的打开方式(成员函数open)
std::ios::in:以输入(读)模式打开文件。std::ios::out:以输出(写)模式打开文件。如果文件已存在,其内容会被清空,除非同时使用了std::ios::app。std::ios::binary:以二进制模式打开文件,而非文本模式。std::ios::ate:打开文件并直接定位到文件末尾。std::ios::app:追加模式。所有写入都会追加到文件末尾。std::ios::trunc:如果文件已经存在,先删除文件然后重新创建。
例
// 以只读模式打开文件
std::ifstream inFile;
inFile.open("example.txt", std::ios::in);// 以写模式打开文件,如果文件存在则清空内容
std::ofstream outFile;
outFile.open("example.txt", std::ios::out);// 以读写模式打开文件,如果文件不存在则创建
std::fstream ioFile;
ioFile.open("example.txt", std::ios::in | std::ios::out | std::ios::trunc);// 以二进制追加模式打开文件
std::ofstream appendFile;
appendFile.open("example.bin", std::ios::binary | std::ios::app); 
文件读写操作
文件写入操作
1.使用插入运算符(<<):这是最常用的向文件写入数据的方法。它允许将数据项直接插入到输出文件流中。
    std::ofstream outFile("example.txt");outFile << "Hello, world!" << std::endl;outFile << 123 << std::endl;
 
 
2.使用成员函数write():对于二进制文件写入,可以使用write()函数。此函数允许以二进制格式直接写入内存中的数据。
    int data = 123;outFile.write(reinterpret_cast<const char*>(&data), sizeof(data));
 
 
文件读取操作
3.使用提取运算符(>>):这是最常用的从文件读取数据的方法。它允许将数据从输入文件流中提取到变量中。
    std::ifstream inFile("example.txt");std::string text;int number;inFile >> text >> number;
 
 
4.使用成员函数getline():getline()用于从文件中读取一行,通常用于读取文本文件。
    std::string line;while (std::getline(inFile, line)) {// 处理每行数据}
 
 
5.使用成员函数read():对于二进制文件读取,可以使用read()函数。此函数允许直接读取二进制格式的数据到内存中。
    int data;inFile.read(reinterpret_cast<char*>(&data), sizeof(data));
 
 
其他操作
- 检查文件末尾(EOF):使用成员函数
eof()来检查是否读到文件末尾。 
    while (!inFile.eof()) {// 读取操作}
 
 
- 检查读写状态:使用成员函数
good(),bad(),fail(),eof()来检查文件流的状态。 
    if (inFile.good()) {// 文件状态良好}
 
 
- 位置定位操作:使用
seekg()(对输入流)和seekp()(对输出流)来定位文件中的位置,使用tellg()和tellp()来获取当前位置。 
    inFile.seekg(0, std::ios::end);  // 移动到文件末尾long fileSize = inFile.tellg();  // 获取文件大小 
stringstream
stringstream 是 C++ 标准库中 <sstream> 头文件提供的一个非常有用的类,它允许字符串对象像流(stream)一样被操作。这意味着你可以使用类似于文件和控制台输入输出流的方式来处理字符串数据。stringstream 主要用于字符串的解析和格式化。
基础用法
1.包含头文件:
    #include <sstream>
 
 
2.声明 stringstream 对象:
    std::stringstream ss;
 
 
3.向流中写入数据:可以使用流插入运算符 << 向 stringstream 对象中插入字符串或数值等数据。
    ss << "Example " << 123 << " " << 45.67;
 
 
4.从流中读取数据:可以使用流提取运算符 >> 从 stringstream 对象中提取数据到变量中。
    std::string str;int intValue;double doubleValue;ss >> str >> intValue >> doubleValue;
 
 
5.访问 stringstream 的字符串:可以使用 str() 成员函数获取流当前的字符串值,或者将一个新的字符串值分配给流。
    std::string currentString = ss.str();  // 获取当前字符串ss.str("New string");  // 分配新的字符串
 
 
高级用途
- 数据解析:当处理复杂格式的字符串时,
stringstream可以非常便利地解析其中的不同部分到相应的变量中。 
    std::string data = "John Doe 30 175.5";std::stringstream parser(data);std::string firstName, lastName;int age;float height;parser >> firstName >> lastName >> age >> height;
 
 
- 复杂格式化:使用 
stringstream可以很容易地将多种类型的数据合并到一条字符串中,并进行复杂的格式化。 
    std::stringstream formatter;formatter << "Name: " << firstName << " " << lastName << ", Age: " << age << ", Height: " << height;std::string result = formatter.str();