2016-04-23 31 views
0

名字和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; 
} 
+0

是“名称”总是一个字,也可以是多个单词(像一个“真正”的名字)? –

+1

此外,你可能想阅读[“为什么iostream :: eof内循环条件认为是错误的?”](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-条件考虑的,是错误的)。 –

+0

名称将永远是一个单词(在我的情况下) – CodingPoding

回答

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

嗯......但是,在第一个if语句中,该列表只存在于一个循环中,然后我甚至可以使用它,否则该节点将不得不被添加到列表中,然后该列表必须是添加到主列表中 – CodingPoding

+0

@CodingPoding添加了一些伪代码来显示如何处理列表。 –

+0

噢好吧,这绝对有帮助。但你是什么意思current_list = CreateList(); – CodingPoding

相关问题