名字和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