【问题标题】:searching for values in a text file在文本文件中搜索值
【发布时间】:2016-05-02 11:11:54
【问题描述】:

知道我的一个同学已经就这个主题发布了一个类似的问题,但我仍然无法理解这应该如何工作。 这是包含虚假学生信息的文件设置:

918273645,Steve,Albright,ITCS2530,MATH210,ENG140
123456789,Kim,Murphy,ITCS2530,MATH101
213456789,Dean,Bowers,ITCS2530,ENG140
219834765,Jerry,Clark,MGMT201,MATH210

由于某种原因,我只能读取文本文件的第一行,而不能读取下面的任何行。我需要弄清楚如何读取每行的前 9 个字符并将它们与用户输入进行比较。然后结转该行的其余部分。但无法弄清楚我哪里出错了。

这是我目前所拥有的:

void Login()
{

    Student NewStudent;
    ifstream inFile;
    ifstream outFile;
    string inFileName = "C:\\Users\\Prophet\\Desktop\\registration.txt";
    string outFileName = "C:\\Users\\Prophet\\Desktop\\registration.txt";
    openInputFile(inFile, inFileName);



    while (true)
    {
        cout << "Please enter your student ID\n" << endl;
        cin >> NewStudent.StudentID;

        if (NewStudent.StudentID.length() == 9)
            break;
        else
            cout << "That ID is invalid - IDs are 9 digits" << endl;
    }



    if (inFile.is_open())
    {


        while (!inFile.eof())

        {
            string line;
            while (getline(inFile, line))
            {
                stringstream ss(line);

                string StudentID, FirstName, LastName, ListOfCourses;
                getline(ss, StudentID, ',');
                getline(ss, FirstName, ',');
                getline(ss, LastName, ',');
                getline(ss, ListOfCourses, ','); 
                cout << "\n";
                {
                    if (StudentID == NewStudent.StudentID)
                    {
                        cout << "Welcome to the Macomb Community College enrolment system " << FirstName << " " << LastName << endl;
                        inFile.close();
                        MainMenu();

                    }
                    if (StudentID != NewStudent.StudentID)
                    {
                        cout << "Welcome New student" << endl;
                        cout << "Please enter yuour first name: ";
                        cin >> NewStudent.FirstName;
                        cout << "Please enter yuour last name: ";
                        cin >> NewStudent.LastName;
                        outFile.open("C:\\Users\\Prophet\\Desktop\\registration.txt");
                        openOutputFile(outFile, outFileName);
                        MainMenu();


                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 请发帖minimal reproducible example。例如,MainMenu() 实际上是什么?
  • 您的代码仅读取一门课程(作为字符串),而在您的输入文件中有一个课程“列表”。因此,从该文件中读取的下一个学生 ID 实际上是一门课程。
  • MainMenu() 只是一系列 switch 语句。我无法弄清楚为什么我无法阅读第一行中的所有信息,例如课程列表。以及为什么我无法阅读下一行。我已经尝试了大约一周。

标签: c++ arrays string fstream


【解决方案1】:

在读取文件的主循环中,StudentID == NewStudent.StudentID 时中断,StudentID != NewStudent.StudentID 时中断,这意味着您总是在读取第一行后结束循环。

从基于行的记录中读取字段时,首先读取行并将其存储在字符串流中然后从那里读取字段总是更容易。但这无济于事,除非您将代码更改为仅在找到 id 时停止。

注意while (!inFile.eof()) 可能会给出错误的结果。你最好检查一下已经提到的读取结果many times on this site

【讨论】:

  • 谢谢,我删除了 break 语句并添加了一个字符串流,但我仍然无法弄清楚这应该是什么样子。还是只看第一行
  • 您应该首先阅读所有行以检查是否可以找到ID。只有在读取了所有行之后,您才能确定没有找到 ID,如果是这种情况,您必须追加一行并且不覆盖整个文件。
【解决方案2】:

您当前在循环中使用break,因此在第一行之后它不会进入第二行。您必须使用continue 而不是break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2013-06-27
    • 2023-03-28
    相关资源
    最近更新 更多