如何通过C++检查文本文件中每行的特定单词

How to check for a specific word per line in a text file through C++?

本文关键字:单词 C++ 何通过 检查 文本 文件      更新时间:2023-10-16

名称和 RUID 从 read.txt 中读取,如果它在 中找到字符串"RUID" read.txt,您需要创建一个新的链接列表来存储以下内容 RUID 和名称(字符串中有多个 RUID),完成阅读 read.txt中的所有元素,使用运算符重载+连接 链接列表在一起

这就是对我的要求。我已经知道如何打开文本文件并设法执行其他所有操作,包括运算符重载。我只是对如何尝试将文本文件中的信息获取和存储到单独的链表中感到困惑。

这是我的伪代码(我只是不知道如何完全实现它)

while (!File.eof())
{
if (a string in a line = RUID)
{
make a new LinkedList and set as current LinkedList
jump to next line
read line into the LinkedList
}
}

这是我的文本文件(忽略每行之间的空格)

鲁伊德名称

4325名称1

鲁伊德名称

5432名称2

6530名称3

鲁伊德名称

1034名称4

2309名称5

这是我到目前为止的代码

#include "LinkedList.h"
#include <string>
#include <algorithm>
#include <iostream>
#include <time.h>
#include "stdlib.h"
#include <cmath>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
    LinkedList x;
    string line;
    ifstream ReadFile;
    ReadFile.open("read.txt");
    int counter = 0;
    if (ReadFile.is_open())
    {
        while (!ReadFile.eof())
        {
        }
    }
    ofstream WriteFile("Write.txt");
    if (WriteFile.is_open())
    {
        Node* extra;
        int inf = 9;
        while (inf != 10)
        {
            cout << "Select the following options:" << endl;
            cout << "1. Add node to the list" << endl;
            cout << "2. Remove node from list" << endl;
            cout << "3. Print list" << endl;
            cout << "4. Print element of the list" << endl;
            cout << "5. Sort the list" << endl;
            cout << "6. Quit" << endl;
            int option;
            cin >> option;
            cout << "Selection: " << option << endl;
            if (option == 1)
            {
                cout << "Enter student name: ";
                string insert;
                cin.ignore();
                getline(cin, insert);
                int temp = rand() % 9000 + 1000;
                extra = new Node(insert, temp);
                x.addNode(extra);
                cout << endl;
                x.printList();
            }
            else if (option == 2)
            {
                cout << "Enter the RUID to be remove: ";
                int remove;
                cin >> remove;
                cout << endl;
                x.removeNode(remove);
                x.printList();
            }
            else if (option == 3)
            {
                x.printList();
            }
            else if (option == 4)
            {
                cout << "Enter the index: ";
                int index;
                cin >> index;
                cout << endl;
                x.printElement(index);
            }
            else if (option == 5)
            {
                x.sortList();
                x.printList();
            }
            else if (option == 6)
            {
                break;
            }
            else
            {
                cout << "Invalid output" << endl;
            }
        }
    }
    system("pause");
    return 0;
}

简单地读作循环中的两个字符串。如果第一个等于 "RUID"执行启动新列表所需的任何特殊处理,否则将其转换为整数并添加到当前列表中。

示例(包括用于列表处理的伪代码):

ListType* current_list = nullptr;
std::string ruid_string, name;
while (ReadFile >> ruid_string >> name)
{
    if (ruid_string == "RUID")
    {
        // Logic to create a new list or whatever
        if (current_list != nullptr)
        {
            // A list already exists, add it to the "master" list
            master_list.AddSubList(current_list);
        }
        current_list = CreateList();
    }
    else
    {
        int ruid = std::stoi(ruid_string);
        // Add node to current list
        if (current_list != nullptr)
            current_list->AddNode(ruid, name);
    }
}
if (current_list != nullptr)
{
    // Add the last list to the master list
    master_list.AddSubList(current_list);
}