mysql存储的文件结构体_C++文件读写结构体详解

本文介绍了C++中结构体如何用于组织和存储信息,特别是在模拟MySQL存储文件结构时的应用。通过示例代码展示了如何使用结构体创建和读取包含个人详细信息的二进制文件,强调了在处理包含不同类型数据的结构时使用ios::binary模式的重要性。
摘要由CSDN通过智能技术生成

我们知道,一个字段是与单个项目有关的单块信息。而记录由字段组成,是关于单个项目的完整信息集。例如,一组字段可能是一个人的姓名、年龄、地址和电话号码,而所有这些与一个人有关的字段则共同组成了一个记录。

在 C++ 中,结构体提供了一种将信息组织到字段和记录中的便捷方法。例如,下面的结构体声明可以用来创建一个包含某人信息的记录:

const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;

struct Info

{

char name[NAME_SIZE];

int age;

char address1[ADDR_SIZE];

char address2[ADDR_SIZE];

char phone[PHONE_SIZE];

};

除了提供信息的组织结构之外,结构体还可以将信息打包成一个单元。例如,假设结构体变量 person 被声明如下:

Info person;

一旦 person 的成员(或字段)填充了信息,则可以使用 write 函数将整个变量写入一个文件中:

file.write(reinterpret_cast(Sperson), sizeof(person));

第一个实参是 person 变量的地址。reinterpret_cast 转换操作符是必需的,因为 write 需要第一个实参是一个指向 char 的指针。如果将除 char 之外的其他任何东西的地址传递给 write 函数,则必须使用转换操作符使它看起来像是一个指向 char 的指针。第二个实参是 sizeof 运算符,它告诉 write 有多少个字节要写入文件。

下面的程序演示了这种技术:

//This program demonstrates the use of a structure variable to

//store a record of information to a file.

#include

#include

#include

#include // for toupper

using namespace std;

const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;

struct Info

{

char name[NAME_SIZE];

int age;

char address1[ADDR_SIZE];

char address2[ADDR_SIZE];

char phone[PHONE_SIZE];

};

int main()

{

Info person; // Store information about a person

char response; // User response

string input; // Used to read strings

// Create file object and open file

fstream people("people.dat", ios::out | ios::binary);

if (!people)

{

cout << "Error opening file. Program aborting.\n";

return 0;

}

// Keep getting information from user and writing it to the file in binary mode

do {

cout << "Enter person information:\n";

cout << "Name: ";

getline (cin, input);

strcpy(person.name, input.c_str());

cout << "Age:";

cin >> person.age;

cin.ignore(); // Skip over remaining newline

cout << "Address line 1: ";

getline(cin, input);

strcpy(person.address1, input.c_str());

cout << "Address line 2: ";

getline(cin, input);

strcpy(person.address2, input.c_str());

cout << "Phone : ";

getline(cin, input);

strcpy(person.phone, input.c_str());

people.write(reinterpret_cast(&person), sizeof(person));

cout << "Do you want to enter another record? ";

cin >> response;

cin.ignore();

} while (toupper(response) == 'Y');

// Close file

people.close ();

return 0;

}

程序输出结果:

Enter person information:

Name: http://c.biancheng.net

Age:5

Address line 1: no1

Address line 2: no2

Phone : 123456

Do you want to enter another record? N

注意,由于结构可以包含混合的数据类型,所以当打开一个文件来存储它们时,应该总是使用 ios::binary 模式。

此程序允许通过填充 person 变量的成员来创建一个文件,然后把变量写入文件。要将 C 字符串读入一个数组,则程序首先应使用 getline 函数读取字符串对象,然后使用 strcpy 将 C 字符串移动到字符数组中。下面的程序打开文件并将每个记录读入 person 变量,然后在屏幕上显示该信息:

// This program demonstrates the use of a structure

// variable to read a record of information from a file.

#include

#include

using namespace std;

const int NAME_SIZE = 51, ADDR_SIZE = 51, PH〇NE_SIZE = 14; struct Info {

char name[NAME_SIZE];

int age;

char addressl[ADDR_SIZE];

char address2 [ADDR_SIZE];

char phone[PHONE_SIZE];

};

int main()

{

Info person; // Store person information

char response; // User response

// Create file object and open file for binary reading

fstream people("people.dat", ios::in | ios:ibinary);

if (!people)

{

cout << "Error opening file. Program aborting.\n";

return 0;

}

// Label the output

cout << "Here are the people in the file:\n\n";

// Read one structure at a time and echo to screen

people.read(reinterpret_cast(&person), sizeof (person));

while (!people.eof())

{

cout << "Name: ";

cout << person.name << endl;

cout << "Age: ";

cout << person.age << endl;

cout << "Address line 1: ";

cout << person.address1 << endl;

cout << "Address line 2: ";

cout << person.address2 << endl;

cout << "Phone: ";

cout << person.phone << endl;

cout << "\nStrike any key to see the next record.\n";

cin.get (response);

people.read(reinterpret_cast(&person), sizeof(person));

}

cout << "That's all the information in the file!\n";

people.close();

return 0;

}

程序输出结果:

Here are the people in the file:

Name: http://c.biancheng.net

Age: 5

Address line 1: no1

Address line 2: no2

Phone: 123456

Strike any key to see the next record.

That's all the information in the file!

注意,如果结构中包含指针,则使用本节介绍的技术将无法正确存储到磁盘。这是因为如果在程序的后续运行中将结构读入内存中,则不能保证所有程序变量都位于相同的内存位置。由于 string 类对象包含隐式指针,所以它们不能作为将要存储的结构的一部分。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值