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

时间:2016-04-23 00:08:15

标签: c++ file c++11 linked-list singly-linked-list

  

如果发现字符串'RUID',则从read.txt读取名称和RUID   read.txt,您需要创建一个新的Linkedlist来存储以下内容   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
}
}

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

RUID名称

4325 name1

RUID名称

5432 name2

6530 name3

RUID名称

1034 name4

2309 name5

这是我迄今为止的代码

#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;
}

1 个答案:

答案 0 :(得分:0)

只需在循环中读取两个字符串即可。如果第一个等于"RUID",请执行开始新列表所需的任何特殊处理,否则convert it to an integer并添加到当前列表。

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

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);
}