首页 > 解决方案 > 即使将数据保存在文件中,该文件处理代码也不会从文件中读取,这是怎么回事?

问题描述

我正在做一个大学项目,这是我开发的铁路票务系统。现在,问题是数据正在保存在创建的文件中,但是当我想从文件中读取该数据时,控制台窗口中没有任何显示。我尝试过多次更改它,但问题仍然存在。我似乎无法找到解决方案,如果你们能提供帮助,那就太好了。

else if (choice == 4)
{
    char b[13];
    cout << "\n\nPress 1 to display Rawalpindi to Lahore Queue";
    cout << "\nPress 2 to display Lahore to Karachi Queue";
    cout << "\nPress 3 to display Rawalpindi to Karachi Queue" << endl;
    cin >> fchoice;
    if (fchoice == 1)
    {
        char b[] = "rwptolhr.txt";
    }
    else if (fchoice == 2)
    {
        char b[] = "lhrtokch.txt";
    }
    else if (fchoice == 3)
    {
        char b[] = "rwptokch.txt";
    }

    fstream file;
    file.open(b, ios::in | ios::app);
    if (!file)
    {
        cout << "\n\nError in opening file!!!" << endl;
    }

    cout << "\n\nFile content: " << endl;
    //reading and extracting data from file. 
    for (string line; getline(file, line);)
    {
        while (string::npos)
        {
            int count = 0;  //to count the number of name characters before space
            for (int i = 0; line[i] != ' '; i++)
            {
                count++;
            }

            string customername = line.substr(0, count);
            cout << "\n\nName: " << customername << endl;

            int fcount = 0; //to count the number of age characters before space
            for (int i = count + 1; line[i] != ' '; i++)
            {
                fcount++;
            }   //please note that "+1 +2 etc in the counter    //variables" are given because there is spaces in the string.
            string customerfrom = line.substr(count + 1, fcount);
            cout << "From: " << customerfrom << endl;

            int tcount = 0; //to count the program characters
            for (int i = count + fcount + 2; line[i] != ' '; i++)
            {
                tcount++;
            }

            string customerto = line.substr(fcount + count + 2, tcount);
            cout << "To: " << customerto << endl;

            int pcount = 0; //to count the number of name characters before space
            for (int i = count + fcount + tcount + 3; line[i] != ' '; i++)
            {
                pcount++;
            }

            string customerpayment = line.substr(count + fcount + tcount + 3, pcount);
            cout << "Payment: " << customerpayment << endl;

            int ocount = 0; //to count the number of name characters before space
            for (int i = count + fcount + tcount + pcount + 4; line[i] != '\0'; i++)
            {
                ocount++;
            }

            string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
            cout << "Order id: " << customeroid << endl;
            break;

        }
    }

}

标签: c++file-handling

解决方案


更改char b[13]string b对我有用。

else if (choice == 4)
        {
            string b;
            cout << "\n\nPress 1 to display Rawalpindi to Lahore Queue";
            cout << "\nPress 2 to display Lahore to Karachi Queue";
            cout << "\nPress 3 to display Rawalpindi to Karachi Queue" << endl;
            cin >> fchoice;
            if (fchoice == 1)
            {
                b = "rwptolhr.txt";
            }
            else if (fchoice == 2)
            {
                b = "lhrtokch.txt";
            }
            else if (fchoice == 3)
            {
                b = "rwptokch.txt";
            }

            fstream file;
            file.open(b, ios::in | ios::app);
            if (!file)
            {
                cout << "\n\nError in opening file!!!" << endl;
            }

            cout << "\n\nFile content: " << endl;
            //reading and extracting data from file. 
            for (string line; getline(file, line);)
            {
                while (string::npos)
                {
                    int count = 0;  //to count the number of name characters before space
                    for (int i = 0; line[i] != ' '; i++)
                    {
                        count++;
                    }

                    string customername = line.substr(0, count);
                    cout << "\n\nName: " << customername << endl;

                    int fcount = 0; //to count the number of age characters before space
                    for (int i = count + 1; line[i] != ' '; i++)
                    {
                        fcount++;
                    }   //please note that "+1 +2 etc in the counter    //variables" are given because there is spaces in the string.
                    string customerfrom = line.substr(count + 1, fcount);
                    cout << "From: " << customerfrom << endl;

                    int tcount = 0; //to count the program characters
                    for (int i = count + fcount + 2; line[i] != ' '; i++)
                    {
                        tcount++;
                    }

                    string customerto = line.substr(fcount + count + 2, tcount);
                    cout << "To: " << customerto << endl;

                    int pcount = 0; //to count the number of name characters before space
                    for (int i = count + fcount + tcount + 3; line[i] != ' '; i++)
                    {
                        pcount++;
                    }

                    string customerpayment = line.substr(count + fcount + tcount + 3, pcount);
                    cout << "Payment: " << customerpayment << endl;

                    int ocount = 0; //to count the number of name characters before space
                    for (int i = count + fcount + tcount + pcount + 4; line[i] != '\0'; i++)
                    {
                        ocount++;
                    }

                    string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
                    cout << "Order id: " << customeroid << endl;
                    break;

                }
            }



        }

推荐阅读