Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取文本文件以c++;_C++ - Fatal编程技术网

C++ 读取文本文件以c++;

C++ 读取文本文件以c++;,c++,C++,我有一个函数,它应该读到文本文件的末尾,检索数据并将数据存储在一些变量中 名字姓氏国籍客人-经济舱# dd 2350 102 RIC Thing USA 2350 104 然后我使用另一个函数尝试修改数据。您可以看到变量的数据位置,但当我试图比较数据时,会说找不到 它发现了104个 请输入您要更改的房间号:104 名字姓氏国籍客人-房间# 这两张照片是打印出来的,这样我就可以看到取回了什么 102 104 RIC Thing USA 2350 104 但是找不到102 请输入您要更改的房间号:1

我有一个函数,它应该读到文本文件的末尾,检索数据并将数据存储在一些变量中

名字姓氏国籍客人-经济舱# dd 2350 102 RIC Thing USA 2350 104

然后我使用另一个函数尝试修改数据。您可以看到变量的数据位置,但当我试图比较数据时,会说找不到

它发现了104个

请输入您要更改的房间号:104

名字姓氏国籍客人-房间# 这两张照片是打印出来的,这样我就可以看到取回了什么 102 104

RIC Thing USA 2350 104

但是找不到102

请输入您要更改的房间号:102

名字姓氏国籍客人-房间# 这两张照片是打印出来的,这样我就可以看到取回了什么 102 104

对不起,找不到匹配项

请确保键入的房间号在修改之前已预订

void HotelRoom::modifyresroom()
{
    int occup;
    string roomtochange;
    string guestroomdb;
    int newaccupancy;
    double newcost;
    char savinf;
    string fname, lname, nationality;
    string checkaddroom;
    ifstream getdatafromaddroom; // creation of the ifstream object
    getdatafromaddroom.open("reserveroom.out");

    cout << "Please enter the room number you would like to make the changes to : ";
    cin >> roomtochange;

    cout << endl;

    if (getdatafromaddroom.fail()) // if statement used for error
    // checking
    {
        cout << "Could not open file" << endl; // message that will be
    }
    cout << "First Name" << '-' << "Last Name" << '-' << "Nationality" << '-' << "Guest(s)" << '-' << "Room #" << endl;
    cout << "-------------------------------------------------------" << endl;

    while (!getdatafromaddroom.eof()) {
        getdatafromaddroom >> fname >> lname >> nationality >> occup >> cost >> guestroomdb;
        if (getdatafromaddroom.eof()) {
            break;
        }
        cout << guestroomdb << endl;
    }
    //if statement begin by error checking
    if (roomtochange != guestroomdb) {

        cout << "SORRY...NO MATCH FOUND." << endl;
        //cout << setw(5) << fname << ' ' << setw(10) << lname << ' ' << setw(10) <<nationality << ' ' << setw(10) << occup<< ' ' << setw(9) << cost << ' ' << setw(9) << guestroomdb <<endl;
        char backtomod;
        cout << endl;
        cout << "Please ensure that the room number typed was  was booked before it can be modified." << endl;
        cout << endl;
        cout << "1 - Enter another room number, 2 - Back to the mail menu : ";
        cin >> backtomod;

        //switch block used to allow the user to reenter the room number or go back to the mail menu
        switch (backtomod) {
        case '1':
            HotelRoom::modifyaddromm();
            break;
        case '2':
            HotelRoom::hotelmenu();
            break;
        default:
            cout << endl;
            cout << "(--" << backtomod << "--)"
                 << " wasn't a valid selection. you will now be directed to the main menu.";
            system("Pause");
            cout << endl;
            HotelRoom::hotelmenu();
            break;
        }
    }
    else {

        // if room number is found t will be printed out below

        cout << setw(5) << fname << ' ' << setw(10) << lname << ' ' << setw(10) << nationality << ' ' << setw(10) << occup << ' ' << setw(9) << cost << ' ' << setw(9) << guestroomdb << endl;
    }

    cout << "How many person should be in the new room? : ";
    cin >> newaccupancy;
    newcost = newaccupancy * HotelRoom::getdailyrate();
    cout << endl;
    cout << "[y] to save : ";
    cin >> savinf;

    cout << fname << ' ' << lname << ' ' << nationality << ' ' << newaccupancy << ' ' << newcost << ' ' << guestroomdb << endl;
    if (savinf == 'y' || savinf == 'Y') {
        ofstream editrecord;
        editrecord.open("reserveroom.out", ios::app);
        editrecord << fname << ' ' << lname << ' ' << nationality << ' ' << newaccupancy << ' ' << newcost << ' ' << guestroomdb << endl;
        editrecord.close();
        getdatafromaddroom.close();
        hotelmenu();
    }
    else {
        cout << "The changes made weren't saved.";
        system("Pause");
        hotelmenu();
    }
    //}
}
void HotelRoom::modifyresroom()
{
内占位;
字符串空间更改;
字符串客房;
int NEVACUPANCY;
双倍新成本;
char-savinf;
字符串fname、lname、国籍;
字符串校验室;
ifstream getdatafromaddroom;//创建ifstream对象
getdatafromaddroom.open(“reserveroom.out”);
改变的空间;

请不要发布文本图片。while(!getdatafromaddroom.eof())感谢您提供的链接。您能告诉我为什么即使检索到了数据,也表示找不到。您有一个循环,可以读取整个文件(或多或少)。只有文件中的最后一项将保留在内存中,其余项将被后续读取覆盖。我将从这里开始调查错误。在本应从文件中读取所有记录的循环中,您不断重复覆盖相同的变量。只有最后一条记录将保留在其中,其他记录将消失。您需要如果您以后想在所有记录中找到一些数据,那么这是一种保存所有记录的方法。
bool found = false;
while (getdatafromaddroom >> fname >> lname >> nationality >> occup >> cost >> guestroomdb) {
    if (guestroomdb == roomtochange) {  // stop when you found the
        found = true;                   // record you are interested in
        break;
    }
}

if (!found) {  // found is still false? No such record in the file.
    // handle error