当前位置: 首页> 汽车> 维修 > 南宁建企业网站_网站定制服务_北京搜索引擎优化经理_百度安全中心

南宁建企业网站_网站定制服务_北京搜索引擎优化经理_百度安全中心

时间:2025/7/9 4:28:37来源:https://blog.csdn.net/m0_57667919/article/details/143218642 浏览次数: 0次
南宁建企业网站_网站定制服务_北京搜索引擎优化经理_百度安全中心

目录

C++流

读写文件:文件流

对文本文件流读写 

写文本文件

读文本文件

对二进制文件流读写

写二进制文件

 读二进制文件

 对文件流按格式读写取数据

按指定格式写文件

按指定格式读文件


C++流

IO: 向设备输入数据和输出数据

C++的IO流

设备: 

  1. 文件
  2. 控制台
  3. 特定的数据类型(stringstream)

c++中,必须通过特定的已经定义好的类, 来处理IO(输入输出)

读写文件:文件流

文件流: 对文件进行读写操作

头文件:  <fstream>

类库:

   ifstream    对文件输入(读文件)

   ofstream    对文件输出(写文件)

   fstream     对文件输入或输出

对文本文件流读写 

文件打开方式:

以上打开方式, 可以使用位操作 |  组合起来

写文本文件

(自编代码)

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main(void) {ofstream writefile;writefile.open("flie.txt");string name;int age;while (1) {cout << "please input your name:[Ctrl+z exit]";cin >> name;if (cin.eof()) {break;}writefile << name << "\t";cout << "please input your age:";cin >> age;writefile << age << endl;}writefile.close();system("pause");return 0;
}

读文本文件

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main(void) {ifstream outfile;outfile.open("flie.txt");string name;int age;while (1) {outfile >> name;if (outfile.eof()) {break;}cout << name << "\t";outfile >> age;cout << age << endl;}outfile.close();system("pause");return 0;
}

对二进制文件流读写

思考:

文本文件和二进制文件的区别?

文本文件: 写数字1,  实际写入的是 ‘1’

二进制文件:写数字1, 实际写入的是  整数1(4个字节,最低字节是1, 高3个字节都是0)

                     写字符‘R’实际输入的还是‘R’

写二进制文件

使用文件流对象的write方法写入二进制数据.

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main(void) {ofstream bitWriteFile;bitWriteFile.open("flie.bat");string name;int age;while (1) {cout << "please input your name [Ctrl+z exit] :";cin >> name;if (cin.eof()) {break;}bitWriteFile << name << "\t";cout << "please input your age :";cin >> age;bitWriteFile.write((char*)&age, sizeof(age));}bitWriteFile.close();system("pause");return 0;
}
bitWriteFile.write((char*)&age, sizeof(age));

这行代码是在C++程序中将一个整数 `age` 以二进制形式写入到文件中的操作。让我们详细解释一下这行代码的各个部分:

1. `bitWriteFile`:这是一个 `ofstream` 类型的对象,它是C++标准库中的一个类,用于提供对文件的输出功能。

2. `.write`:这是 `ofstream` 对象的一个成员函数,用于写入数据。`.write` 函数接受两个参数:第一个参数是一个指向要写入数据的指针,第二个参数是要写入的数据的大小(以字符为单位)。

3. `(char*)&age`:这是一个类型转换操作,它将整数 `age` 的地址转换为一个 `char*` 类型的指针。这样做的原因是 `.write` 函数需要一个指向字符数据的指针。由于C++中的类型安全性,直接传递一个整数的地址是不合法的,因此需要将其转换为 `char*` 类型。`&age` 获取 `age` 变量的内存地址,然后通过强制类型转换 `(char*)`,告诉编译器将这个地址当作字符数组的首地址来处理。

4. `sizeof(age)`:这是一个编译时运算符,它返回变量 `age` 所占的字节数。对于一个 `int` 类型的变量,`sizeof(age)` 通常返回 4(在32位系统中)或 8(在64位系统中),这取决于编译器和操作系统的架构。这个值告诉 `.write` 函数需要写入多少字节的数据。

综合起来,`bitWriteFile.write((char*)&age, sizeof(age));` 这行代码的作用是以二进制形式将 `age` 变量的内容写入到 `bitWriteFile` 指定的文件中。这种方式写入的数据不是以文本形式存储的,而是直接存储 `age` 变量在内存中的二进制表示。这意味着,如果你在不同的机器或不同的编译器上读取这个文件,可能会遇到字节序(endianness)或整数大小不一致的问题,导致读取的值不正确。因此,使用二进制文件进行数据交换时,需要确保写入和读取数据的系统环境是兼容的。

 读二进制文件

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main(void) {ifstream bitOutFile;bitOutFile.open("flie.bat", ios::in | ios::binary);string name;int age;while (1) {bitOutFile >> name;if (bitOutFile.eof()) {break;}cout << name << "\t";char temp;bitOutFile.read((char*)&temp,sizeof(temp));bitOutFile.read((char*)&age, sizeof(age));cout << age << endl;}bitOutFile.close();system("pause");return 0;
}

 对文件流按格式读写取数据

使用stringstream

按指定格式写文件

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>using namespace std;int main()
{string name;int age;ofstream outfile;outfile.open("file.txt", ios::out | ios::trunc);while (1) {cout << "请输入姓名: [ctrl+z退出] ";cin >> name;if (cin.eof()) { //判断文件是否结束break;}cout << "请输入年龄: ";cin >> age;stringstream s;s << "name:" << name << "\t\tage:" << age << endl;outfile << s.str();}// 关闭打开的文件outfile.close();system("pause");return 0;
}

按指定格式读文件

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <Windows.h>using namespace std;int main(void)
{char name[32];int age;string line;ifstream infile;infile.open("file1.txt");while (1) {getline(infile, line);if (infile.eof()) { //判断文件是否结束break;}sscanf_s(line.c_str(), "name:%s\t\tage:%d", name, sizeof(name), &age);cout << "姓名:" << name << "\t\t年龄:" << age << endl;}infile.close();system("pause");return 0;
}

关键字:南宁建企业网站_网站定制服务_北京搜索引擎优化经理_百度安全中心

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: