首页 > 编程笔记 > C++笔记 阅读:1,515

C++文件读写结构体详解

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

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<char*>(Sperson), sizeof(person));

第一个实参是 person 变量的地址。reinterpret_cast<char*> 转换操作符是必需的,因为 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 <iostream>
#include <fstream>
#include <cstring>
#include <cctype> // 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<char *>(&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 <iostream>
#include <fstream>
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<char *>(&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<char *>(&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 类对象包含隐式指针,所以它们不能作为将要存储的结构的一部分。