【问题标题】:How to check for a specific word per line in a text file through C++?如何通过 C++ 检查文本文件中每行的特定单词?
【发布时间】:2016-04-23 00:08:14
【问题描述】:

如果在 read.txt 中找到字符串“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 名称1

RUID 名称

5432 名称2

6530 名称3

RUID 名称

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

【问题讨论】:

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


【解决方案1】:

简单地读取为循环中的两个字符串。如果第一个等于"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);
}

【讨论】:

  • 嗯...但是,在第一个 if 语句中,该列表仅存在一个循环,在我什至可以使用它之前,不,并且必须将节点添加到列表中并且那么必须将该列表添加到主列表中
  • @CodingPoding 添加了一些伪代码来显示如何处理列表。
  • 哦,好吧,这绝对有帮助。但是 current_list = CreateList(); 是什么意思
  • 比如,我会在那里做什么?
猜你喜欢
  • 2011-12-17
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多