无法使用数据结构从文件中读取数据

时间:2016-10-03 18:12:15

标签: c++ data-structures ifstream

我有一个包含租车信息的计划和公司。我正在尝试将其读入并将其显示在终端上。但是,我只是让一家公司打印清楚,而其他公司只打印出垃圾。我希望这些代理商也可以存储5个汽车库存,但是如果没有阅读我的所有信息,我们都不知道如何存储它们。我也只能使用C-Style字符串。

以下是我正在阅读的文件:

Hertz 93619
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1

Alamo 89502
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 112.83 1
2010 Toyota Corolla 50.36 1

Budget 93035
2008 Ford Fiesta 42.48 0
2009 Dodge Charger 55.36 1
2012 Chevy Volt 89.03 0
2007 Subaru Legacy 59.19 0
2010 Nissan Maxima 51.68 1

我需要帮助的代码部分:

#include <iostream>
#include <fstream>
using namespace std;

struct car
{

        char agency[10];
        int zip;
        int year;
        char make[10];
        char model[10];
        float price;
        int available;

} ;

struct agency
{

    char company[10];
    int zip;
    int inventory[5];
};

void menu();


// Main Function
int main ()
{
    // declare variables
    const int carAmount = 15;
    int agencyAmount = 1;
    int choice;
    agency agencyLib[carAmount];
    car carLib[carAmount];
    char filename[10];
    ifstream carInData;
    bool menu1 = false;

    //prompt user for input file
    cout << " Enter file name: ";
    cin >> filename;

    // Start loop menu
    while(menu1 = true)
    {
        menu();
        carInData.open(filename);
        cin >> choice;

        if (carInData.is_open())
        {
            // read list of names into array

            for (int count = 0; count < agencyAmount; count++) 
            {
                carInData >> agencyLib[count].company >> agencyLib[count].zip;
                for (count = 0; count < carAmount; count++)
                {
                    carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
                }

            }

        }

    switch (choice)
    {
        // Case 1 closes menu
        case 1:
            return 0;
            break;
        // Case 2 displays if car is available if 1, unavailable if 0
        case 2:
        // itterate through car array
            for(int count = 0; count < agencyAmount; count++)
            {
                cout << agencyLib[count].company << " " << agencyLib[count].zip << "\n";
                for(int count = 0; count < carAmount; count++)
                {
                    // Displays if car is available or not 
                    /*      if (carLib[count].available == 1)
                    cout << " Available ";
                    else
                        cout << " Unavailable ";
                    */
                    // Display Cars
                    cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << "  " << "\n";
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

作为一般性的初步评论,我认为即使是出于学习目的,这种练习最好让你使用std::strings而不是c-strings和std::vector来保持越来越多的项目。

您的代码有什么问题?

第一个问题是您使用相同的计数器count来填充您的代理商数组和汽车数组。这将使您很快得到一个超出数组边界和损坏内存的计数器。

解决方案:使用2个不同的计数器重写您的循环结构。

下一个问题是您没有识别代理商的汽车清单的结尾。这使得阅读多个代理商变得不切实际:您将在流阅读中遇到失败,这将阻止您在数据中获得任何有用的信息。

解决方案:分析阅读失败以识别从汽车(第一个元素应该是数字)到新机构(第一个元素是字符串)。

此外,您可能有一些字符串比字符数组允许的长,导致进一步的内存损坏。

解决方案:使用iomanip()限制读取的字符数以确定最大宽度。除非您选择std::string

,否则强烈建议您这样做

上一期:变长数组不是标准的C ++特性,即使一些流行的编译器支持它。

解决方案:使用new / delete动态分配或选择此练习使用常量最大大小。

代码段:

改编,没有选择,菜单等,阅读看起来像:

const int carAmount = 30;    // !!!
const int agencyAmount = 10;  // !!!
agency agencyLib[carAmount];
car carLib[carAmount];
ifstream carInData ("test.dat");
int numCar = 0, numAgency = 0;        // !!! shows the real number of items available
int count1, count2;                   // 

cout << "Start reading" << endl;
for (numAgency = numCar = 0; carInData && numAgency < agencyAmount; numAgency++) {
    if (!(carInData >> setw(sizeof(agencyLib[numAgency].company)) >> agencyLib[numAgency].company >> agencyLib[numAgency].zip))
        break;  // if nothing left, exit loop immediately
    for (; numCar < carAmount; numCar++) {
        carInData >> carLib[numCar].year >> setw(sizeof(carLib[numCar].make )) >>carLib[numCar].make 
                                         >> setw(sizeof(carLib[numCar].model))>>carLib[numCar].model 
                                         >> carLib[numCar].price >> carLib[numCar].available;
        if (carInData.fail()) { // here we expect a year, but get an agency string 
            carInData.clear(); 
            break;
        }
        strcpy(carLib[numCar].agency, agencyLib[numAgency].company);
        carLib[numCar].zip = agencyLib[numAgency].zip;
    }
}

随后的显示:

cout << "Display agencies: " << endl; 
for (count1 = 0; count1 < numAgency; count1++) {
    cout << agencyLib[count1].company << " " << agencyLib[count1].zip << "\n";
}
cout << "Cars: " << endl;
for (count2 = 0; count2 < numCar; count2++) {
    cout << carLib[count2].agency << " " << carLib[count2].zip << ": "; 
    cout << carLib[count2].year << " " << carLib[count2].make << " " << carLib[count2].model << " " << carLib[count2].price << "  " << "\n";
}

请注意,代理商和汽车之间没有链接(公共字段除外),因此显示屏只显示两个不同的列表。