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

时间:2014-03-07 23:02:27

标签: c++ file class vector

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

基本上,我有一种将数据存储到.dat文件中的工作方法,newEntry函数,但我似乎无法解决如何将数据一次带回一组矢量的顺序结构。有问题的函数是searchDisplay函数。这是代码:

#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;
}

很抱歉,如果任何编码在缩进时有点偏差,我就会遇到将代码放到文本块上的问题。

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

1 个答案:

答案 0 :(得分:0)

您的第一个问题是打开要写入的数据文件。您正在使用标志“ios :: app”打开它,它应该是“ios :: out”。在这里(gcc版本4.7.2)我用“ios :: app”打开文件时没有输出到文件。 使用“ios :: out”创建一个文件并将数据转储到其中。

第二个问题是您打开并读取文件的行。你错过了“[0]”。检查下面的更正版本:

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

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

这里的主要问题是,编写和读取二进制文件与常规文本文件略有不同,例如,这些文本文件具有行结尾的指示。您需要以不同方式组织数据,这可能非常复杂。试试Serializing with Boost