如何从数据文件读取到向量的结构

How Read from a Data File into a Structure of Vectors

本文关键字:向量 结构 读取 文件 数据      更新时间:2023-10-16

我正在尝试弄清楚如何将保存到.dat文件" customer.dat"的二进制数据存储到向量的结构中。我以为我已经有几次了,但是没有运气。

基本上,我有一种工作方法将数据存储到.dat文件,newentry函数中,但是我似乎无法探讨如何将数据一次设置为一个将数据集中回向向量结构的订单结构。所讨论的函数是搜索Display函数。这是代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype>
using namespace std; 
const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;
struct Client {
char name[NAME_SIZE];
char address1[ADDR_SIZE];
char address2[ADDR_SIZE];
char phone[PHONE_SIZE];
double acctBal;
double lastPay;
};  

//void welcome();
int menu();
int newEntry();
int searchDisplay();
int main() {
    int selection;
    int option = 0;
    int end;
    //welcome();
    while (option != 6) {
        selection = menu();
        if (selection == 1) {
            cin.ignore();
            end = newEntry();
            if (end == 0) {
                return 0;
            }
        } else if (selection == 2) {
            end = searchDisplay();
            if (end == 0) {
                return 0;
            }
        }    
    }
return 0;
}
int menu() {
int selection;
cout << "User please enter the number that corresponds with what you wish to do..."               
         << endl;
    cout << "1)     Add An Entry" << endl
         << "2)     Search for a Specfic Person and erase." << endl;
    cin  >> selection;
    return selection;
}
int newEntry() {
    string input;
    Client person;
    char response = 'y';
    //create file object and open file
    fstream customer("customer.dat", ios::app | ios::binary);
    if (!customer) {
        cout << "Error opening file. Program aborting." << endl;
        return 0;
    }
do {
        cout << "Enter person information:" << endl << endl;
        cout << "Name:                                " << endl;
        getline(cin, input);
        strcpy(person.name, input.c_str());
        cout << endl << person.name;
        cout << endl << "Street Adress (And Apartment Number):" << endl;
        cin  >> person.address1;
        getline(cin, input);
        strcpy(person.address1, input.c_str());
        cout << endl << "City, State, Zipcode:                " << endl;
        cin  >> person.address2;
        getline(cin, input);
        strcpy(person.address2, input.c_str());
        cout << endl << "Phone:                               " << endl;
        cin  >> person.phone;
        getline(cin, input);
        strcpy(person.phone, input.c_str());
        cout << endl << "Account Balance:                     " << endl;
        cin  >> person.acctBal;
        //input validation to ensure a non neg number
        cin.ignore();
        cout << endl << "Last Payment:                        " << endl;
        cin  >> person.lastPay;
        //input validation to ensure a non neg number
        customer.write(reinterpret_cast<char *>(&person), sizeof(person));
        cout << endl << "Do you want to enter another record? (Enter Y for Yes, N for No) " << endl;
        cin >> response;
        cout << "_______________________________________________" << endl << endl;
        if (toupper(response) == 'Y') {
            cin.ignore();
        }
    } while (toupper(response) == 'Y');
    customer.close();
    return 1;
}
/********************************************
 My main problem is with the below function
 ********************************************/
int searchDisplay() {
    vector<Client> store(2);
    Client foo;
    int i = 0;
    fstream customer("customer.dat", ios::in | ios::binary);
    if (!customer) {
        cout << "Error opening file. Program aborting." << endl;
        return 0;
    }
    //MY HOPE WAS THIS WOULD STORE EACH SET OF DATA INTO THE STRUCTURE OF VECTORS
    customer.read(reinterpret_cast<char *>(&store), sizeof (store[0]));
    while (!customer.eof()){
        cout << store[i].name << ":" << endl
             << store[i].address1 << endl
             << store[i].address2 << endl
             << store[i].phone << endl
             << store[i].acctBal << endl
             << store[i].lastPay << endl << endl;
        i++;
        customer.read(reinterpret_cast<char *>(&store), sizeof (store[i]));
    }
    customer.close();
    return 1;
}

对不起,如果任何编码在缩进中有点关闭,我对将代码放在文本块上的方法有问题。

是的,任何帮助都会很棒。这是我第一次与矢量一起使用更多文件类别。

您的第一个问题是打开供书写的数据文件。您目前正在用标志" ios :: app"打开它,当时它应该是" ios :: out"。在此处(GCC版本4.7.2)使用" ios :: app"打开文件时,我没有输出。使用" iOS :: out"它创建一个文件并将数据转储到其中。

第二个问题在您打开并读取文件的行中。您缺少" [0]"。检查以下更正的版本:

customer.read(reinterpret_cast<char *>(&store[0]), sizeof (store[0]));

进行此修改,您可以更接近所需的结果。

这里的主要问题是编写和读取二进制文件与常规文本文件有所不同,例如,有线路结尾的指示。您需要以不同的方式组织数据,这可能很复杂。尝试使用Boost序列化。