无限循环读取文件到数组

时间:2015-04-21 17:27:22

标签: c++ infinite-loop fstream

尝试读取文件时,我有一个无限循环。文件从用户输入中保存,然后使用单独的函数进行读取,最后显示。

这是我的阅读功能。循环没有找到eof();从文件。我看不出是什么问题。没有编译器错误。

void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
    myFile.open("insured.dat", ios::in | ios::binary);                                      // Open myFile
    yourFile.open("uninsured.dat", ios::in | ios::binary);                                  // Open yourFile
    for(int i = 0; i < AMOUNT; i++)
    {
        myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
        yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
        for(i = 0; i < AMOUNT; i++)
        {
            while (myFile)
            {
                cout << "description: ";
                cout << items[i].description << endl;
                cout << "quantity: ";
                cout << items[i].quantity << endl;
                cout << "price: ";
                cout << items[i].price << endl;
                cout << "insured: ";
                cout << items[i].insured << endl;
            }
        }

    }

    myFile.close();
    yourFile.close();

}

5 个答案:

答案 0 :(得分:5)

以下循环永远不会结束:不对其内部的myFile进行修改:

        while (myFile)
        {
            cout << "description: ";
            cout << items[i].description << endl;
            cout << "quantity: ";
            cout << items[i].quantity << endl;
            cout << "price: ";
            cout << items[i].price << endl;
            cout << "insured: ";
            cout << items[i].insured << endl;
        }

您必须阅读while循环内的文件才能结束。

此外,您很可能有变量名称冲突:您有两个for循环使用相同的变量i;这大概不是你想要的。

答案 1 :(得分:4)

您的问题是while(myFile),因为该循环中myFile没有任何变化。目前尚不清楚你要完成的循环是什么意思,所以我不能说用什么来代替它。 (嵌套for循环似乎有问题,但似乎没有数据表。)

答案 2 :(得分:1)

@ScottHunter和@Ekelog的答案已经回答了真正的问题。以下是外围问题。

这些行不对:

myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));

您需要使用:

myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
                                                    // ^^ Drop the &
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
                                                    // ^^ Drop the &

答案 3 :(得分:0)

中尝试此操作

while (!myFile.eof()) { cout << "description: "; cout << items[i].description << endl; cout << "quantity: "; cout << items[i].quantity << endl; cout << "price: "; cout << items[i].price << endl; cout << "insured: "; cout << items[i].insured << endl; }

答案 4 :(得分:0)

见内联评论:

void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
    // since you open and close those files here, you probably
    // want to declare them here instead as a function parameter
    myFile.open("insured.dat", ios::in | ios::binary);                                      // Open myFile
    yourFile.open("uninsured.dat", ios::in | ios::binary);                                  // Open yourFile
    // test in the loop
    for(int i = 0; i < AMOUNT && myFile && yourFile; i++)
    {
        // these two reads do not make sense, the second one
        // will overwrite the data just read by the first one...
        // maybe you meant that one of the file might be smaller?
        // or maybe to compare the results in some ways (in which
        // case you need two arrays)
        myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
        yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));

        // write current result
        cout << "description: ";
        cout << items[i].description << endl;
        cout << "quantity: ";
        cout << items[i].quantity << endl;
        cout << "price: ";
        cout << items[i].price << endl;
        cout << "insured: ";
        cout << items[i].insured << endl;
    }

    myFile.close();
    yourFile.close();    
}

作为旁注:

  • 您的第二个for()循环重用了i变量,这意味着它根本无法正常工作。
  • 正如其他人指出sizeof()错误,您也可以使用sizeof(items[0]),因为所有项目的大小相同。
  • 正如SamIAm所提到的,while()被阻止了,因为文件没有被读取,所以EOF从未实际到达文件。
  • 如注释所示,文件对象可能应该在函数中定义,而不是在外部定义,并作为引用传入。