阅读文本文件

时间:2016-12-01 06:52:30

标签: c++

这是我到目前为止所拥有的。我现在遇到的问题是在我的文本文件中读取和插入我的单词串" word.txt"进入特里树。我使用" void init()"函数来测试树的功能,但它使用了文本文件,那就是我不知道如何

任何想法,请

#include <iostream>
using namespace std;
#include <fstream>
#include <iomanip>
#include <string>



class TrieNode {
public:
    // Initialize your data structure here.
    TrieNode() {
        value = 0;
        for (int i=0;i<26;i++){
            children[i] = NULL;
        }
    }
    int value;
    TrieNode* children[26];
};

class Trie {

private:
    TrieNode*root;
    int count;


public:
    Trie() {
        root = new TrieNode();
        count = 0;
    }

    // Inserts a word into the trie.
    void insert(string s) {
        TrieNode *p = root;
        long int len = s.size();
        for (int i=0;i<len;i++){
            int index = s[i] - 'a';
            if (! p->children[index]){
                p->children[index] = new TrieNode();
            }
            p = p->children[index];
        }
        count++;
        p->value = count;
    }

    // Returns if the word is in the trie.
    // -1 if not in trie and not prefix of anything in trie
    // 0 if not in trie but is a prefix of something in trie
    // 1 if in trie
    int search(string key) {
        TrieNode *p = root;
        long int lenght = key.size();
        for (int i=0;i<lenght;i++){
            int index = key[i] - 'a';
            if (p->children[index]){
                p = p->children[index];
            }
            else{
                return -1;
            }
        }
        if (p->value!=0){
            return 1;
        }
        else{
            return 0;
        }

    }

};


//Game class using a tree
class GhostGame{
private:
    string row;
    ifstream fin;
    string wordSoFar = "";
    string Player1,Player2;
    Trie Tree;
public:
    void ReadFile(){
       ifstream fin("word.txt");

        while (!fin.eof()) { // read file till the end
            fin>>row;
            getline(fin,row);
            cout << row << endl;
            Tree.insert(row);
        }
        //fin.close();
    }
    void init(){
        Tree.insert("ab");
        Tree.insert("acd");
    }


    //start menu
    void StartGame(){
        init();
        cout<<"========================="<<endl;
        cout<<"Welcome to Ghost Game"<<endl;
        cout<<"========================="<<endl;

        //ReadFile();
        while(Tree.search(wordSoFar)!=1){
            cout<< "Player 1 Insert a letter => ";
            cin>> Player1;
            cout<<setw(60)<<"now =  ["<< wordSoFar <<"]"<<endl;
            wordSoFar +=Player1;
            if(Tree.search(wordSoFar)==1){
                cout<< "Player 2 Wins "<<endl;
                break;
            }

            cout<< "Player 2 Insert a letter => ";
            cin>>Player2;
            cout<<setw(60)<<"now =  ["<< wordSoFar<<"]"<<endl;
            wordSoFar += Player2;
            if(Tree.search(wordSoFar)==1){
                cout<< "Player 1 Wins "<<endl;
                break;
            }

        }
    }};



// main driver
int main()
{

    GhostGame G1;
    G1.StartGame();


    return 0;
}

2 个答案:

答案 0 :(得分:1)

使用正则表达式执行此类任务,它们位于头文件<regex>中。关于正则表达式的一些好的教程可以在例如Professional C ++(Wrox)一书中找到。

对于文件阅读,请使用<fstream>中的ifstream类。

答案 1 :(得分:-1)

  • 打开文件
  • 使用fgets或get line函数将文件读入临时字符串
  • 检查第一个字符...确保在比较之前使用tolower

    void findString(char *filename, char ch)
    {
      char temp[100];
      FILE *infile = fopen(filename, "rw");
      while(infile != NULL)
      {
        fgets(temp,100,infile);
    
        if(tolower(temp[0]) == ch)
           cout << temp;
      }
      fclose(infile);
    }