将文本文件中的数据读取到结构数组中

Posted

技术标签:

【中文标题】将文本文件中的数据读取到结构数组中【英文标题】:Reading data from a text file into an array of structs 【发布时间】:2013-04-20 19:01:36 【问题描述】:

我之前看到过这个问题,但我找不到可以帮助我解决问题的答案。这里发生了一些事情。这显示了一个菜单,允许用户:

    查看费用, 更新费用,或 退出以返回上一个菜单。

此信息保存到应输入和输出的数据文件中。尝试查看文件中的数据时,第一个菜单选项有问题。假设已经有一个数据文件C:\danceexpenses.txt 包含此信息:

DJ
100
Site
100
Food
100
Favors
100
Security
100
Ticket printing
100
Etc.
100
etc.
100

程序不会显示信息。它只为每个提供一个空行。我需要添加什么来配置输出吗?

void charges()

    //Structures to hold data
    struct Expenses
    
        string name; 
        double cost;
    ;

    int chargesChoice;
    const int CHARGES_MENU_VIEW = 1, CHARGES_MENU_UPDATE = 2, CHARGES_MENU_QUIT = 3;
    const int NUM_EXPENSES = 8; //number of expenses
    Expenses danceExpenses[NUM_EXPENSES]; //array of structures
    int index; //loop counter

    cout << "\nWhat would you like to do?\n\n"
        << "1. View charges\n"
        << "2. Update charges\n"
        << "3. Return to main menu\n"
        << "Enter your choice: ";
    cin >> chargesChoice;

    while (chargesChoice < CHARGES_MENU_VIEW || chargesChoice > CHARGES_MENU_QUIT)
    
        cout << "\nPlease enter a valid menu choice: ";
        cin >> chargesChoice;       
    

    switch (chargesChoice)
    
        case CHARGES_MENU_VIEW: //Display the expenses data
            myFile.open("c:\\danceexpenses.txt", ios::in); //open file
            if (!myFile)
            
                cout << "Error opening file. Program aborting.\n";
                return;
            
            char output[100];
            cout << "\nHere are the expected expenses for the dance:\n";
            for (index = 0; index < NUM_EXPENSES; index++)
                cout << danceExpenses[index].name << endl << endl;
            myFile.close(); //close file
            break;
        case CHARGES_MENU_UPDATE:
            //get expense data
            myFile.open("c:\\danceexpenses.txt", ios::out); //open file
            if (!myFile)
            
                cout << "Error opening file. Program aborting.\n";
                return;
            
            for (index = 0; index < NUM_EXPENSES; index++)
            
                cout << "Enter the name of the expense: ";
                cin.ignore();
                getline(cin, danceExpenses[index].name);
                cout << "Enter the expected cost for this expense: ";
                cin >> danceExpenses[index].cost;
            

            //Write data to file
            for (index = 0; index < NUM_EXPENSES; index++)
            
                myFile << danceExpenses[index].name << endl;
                myFile << danceExpenses[index].cost << endl;
            
            myFile.close(); //close file
            break;
        case CHARGES_MENU_QUIT:
            showMenu();
            break;
    
    charges();

【问题讨论】:

你的代码似乎有无限尾递归,从长远来看不会带来幸福。 嗯,我没有看到对该文件的任何读取权限。还是做一些遗漏的事情? 【参考方案1】:

如果你的第一个动作是输入1,代码应该输入:

    case CHARGES_MENU_VIEW: //Display the expenses data
        myFile.open("c:\\danceexpenses.txt", ios::in); //open file
        if (!myFile)
        
            cout << "Error opening file. Program aborting.\n";
            return;
        
        char output[100];
        cout << "\nHere are the expected expenses for the dance:\n";
        for (index = 0; index < NUM_EXPENSES; index++)
            cout << danceExpenses[index].name << endl << endl;
        myFile.close(); //close file
        break;

这会打开舞蹈费用文件,但不会从中读取。然后它遍历尚未初始化的费用列表,并关闭输入文件。还有一个未使用的变量output

您需要先将数据读入内存,然后再打印出来。

int num = 0;
while (myFile >> danceexpenses[num].name >> danceexpenses[num].cost)
    num++;

for (index = 0; index < num; index++)
    cout << danceExpenses[index].name << endl << endl;

【讨论】:

以上是关于将文本文件中的数据读取到结构数组中的主要内容,如果未能解决你的问题,请参考以下文章

C ++将文本文件中的数字读取到数组中

将数字从文本文件读取到 C 中用户定义函数中的数组

我如何使用scanf读取文本文件中的下一行

在C++中如何读取文本中的数据存储为数组变量?

如何读取文本文件并将其行添加到 Java 中的数组中?

从文本文件中读取数据并将数据插入到数组中