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
// 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:nn"; // 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!