如何通过C++检查文本文件中每行的特定单词
How to check for a specific word per line in a text file through C++?
名称和 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);
}
相关文章:
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 如何根据单词在文本中出现的概率输出单词
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- C++-字符串是否包含一个带有简单循环的单词
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- C++为一串单词添加空格
- 在C++中查找(奇怪的)字符串中的单词
- 当字符串是某个单词时给出输出?
- 当您在此单词中搜索单词时调整字符数组的大小?
- 数每个单词的元音
- 我们如何在文本文件中找到C++中的几个单词?
- 使用 BFS 的单词梯 2
- 替换字符串中的单词,但忽略引号中的单词