c++ - 如何从数据文件读入 vector 结构

标签 c++ file class vector

我正在尝试弄清楚如何将保存到 .dat 文件“customer.dat”的二进制数据存储到 vector 结构中。我以为我有过几次,但没有运气。

基本上,我有一种将数据存储到 .dat 文件中的工作方法,即 newEntry 函数,但我似乎无法弄清楚如何将数据一次一组地带回 vector 的顺序结构中.有问题的函数是 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;
}

抱歉,如果任何编码的缩进有一点偏差,我对将代码放入文本 block 的方法有疑问。

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

最佳答案

您的第一个问题是打开数据文件进行写入。您当前使用标志“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 .

关于c++ - 如何从数据文件读入 vector 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22262395/

相关文章:

java - 使用java,从文件夹位置提取列表文件名的最资源有效的方法是什么

php - 在 PHP 中如何对文件进行反向排序?

swift - 使用 swift 代码从文件系统打开 pdf 文件

c++ - 如何声明包含整数数组的对象

c++ - Makefile 循环依赖被丢弃

c++ - 如何在 Visual C++ 中将字符串文字转换为无符号字符数组

c++ - 未定义错误构造函数

c# - 交换类实例数组中的值

actionscript-3 - 类之间的命名空间变量

c++ - 破坏 std::stringbuf 导致访问冲突