名字和RUID从read.txt阅读,如果它在 read.txt找到字符串“RUID”,你需要创建一个新的LinkedList存储以下 RUID和名称(有多个RUID在字符串),读完 所有read.txt元素,使用操作符重载+连接 linkedlists在一起如何通过C++检查文本文件中每行的特定单词?
这是什么正在问我的。我已经知道如何打开文本文件并设法做其他事情,包括操作符重载。我只是困惑,我应该如何去尝试从文本文件中获取信息并将其存储到单独的链接列表中。
这是我的伪代码(我只是不知道如何相当实现它)
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 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;
}
是“名称”总是一个字,也可以是多个单词(像一个“真正”的名字)? –
此外,你可能想阅读[“为什么iostream :: eof内循环条件认为是错误的?”](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-条件考虑的,是错误的)。 –
名称将永远是一个单词(在我的情况下) – CodingPoding