用C ++搜索平面文件中的记录

时间:2012-02-19 17:55:31

标签: c++

我用C ++编写了一个片段来搜索flatfile中的记录。但代码不起作用。以下是我写的代码

case 4:
    {
    cout<<"You Entered 4 (This Parses and searches BankAccount flatfile)"<<endl;

    inputFile>>SearchParam;
    cout<<"Enter Search criteria:"<<SearchParam;

        fstream inputFile; //file stream object
        inputFile.open("BankAccount.txt", ios::in);

    struct Bank
{
string AccountName;
string AccountNumber;
string AccountBalance;
}

//Array of bank record entries
Bank Details[20];
//Do until AccountName = SearchParam (Search by AccountName)
        i = 0;
        do{
getline(BankAccount, Details[i].AccountName,' ');
getline(BankAccount, Details[i].AccountNumber,' ');
getline(BankAccount, Details[i].AccountBalance,' ');
i++;
}
while (!BankAccount.eof()&& SearchParam != AccountName)
    }
    break;

2 个答案:

答案 0 :(得分:0)

您的代码无法编译。

但我认为您的问题是您在从文件中加载i并进行比较之间更改AccountName。因此,尚未正确分配所比较的值。

答案 1 :(得分:0)

struct Bank
{
    string AccountName;
    string AccountNumber;
    string AccountBalance;
}

需要分号:

struct Bank
{
    string AccountName;
    string AccountNumber;
    string AccountBalance;
};

'while(...)'需要分号:

while (!BankAccount.eof()&& SearchParam != AccountName);