2016-05-14 102 views
0

我有一個程序分析文本文件中不同詞和總詞的數量,然後將其寫入新的輸出文件。我有第一部分,但我不知道如何讓我的打印功能打印到新的文本文件。將全部單詞或不同單詞打印到新文件上,但Print2()函數似乎不起作用。請看// POINT OF INTEREST //部分,我認爲問題源於此。使用打印功能輸出到一個新文件

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cctype> 
int distinctWord = 0; 
using namespace std; 

typedef int ItemType; 

struct Node { 
    string word; 
    int count = 1; 
    Node* left; 
    Node* right; 
}; 

class TreeType { 
private: 
    Node* root; 
    void Insert(Node*& root, string word); 
    void Destroy(Node*& root); 
    void PrintTree(Node* root); 
    void Print2File(Node* root, ofstream& fout); 
public: 
    TreeType() {root = nullptr;} 
    ~TreeType(); 
    void PutItem(string word); 
    void Print(); 
    void Print2(ofstream& fout); 
}; 

void TreeType::Print() { 
    PrintTree(root); 
    cout << endl; 
} 

void TreeType::PrintTree(Node* root) { 
    if (root == nullptr) 
     return; 
    PrintTree(root->left); 
    cout << root->word << " " << root->count << endl; 
    PrintTree(root->right); 
} 
///////////////POINT OF INTEREST/////////////////////////// 
///////////////POINT OF INTEREST/////////////////////////// 

void TreeType::Print2File(Node* root, ofstream& fout){ 
    if (root == nullptr) 
     return; 
    PrintTree(root->left); 
    fout << root->word << " " << root->count << endl; 
    PrintTree(root->right); 
} 

void TreeType::Print2(ofstream& fout) { 
    Print2File(root, fout); 
    cout << "Printed to another file" << endl; 
} 

///////////////POINT OF INTEREST/////////////////////////// 
///////////////POINT OF INTEREST/////////////////////////// 

void TreeType::PutItem(string word) { 
    Insert(root, word); 
} 

void TreeType::Insert(Node*& root, string word) { 
    if (root == nullptr) { 
     root = new Node; 
     distinctWord++; 
     root->word = word; 
     root->right = nullptr; 
     root->left = nullptr; 
     return; 
    } 
    if(root->word == word){ 
     root->count++; 
     return; 
    }else if (word < root->word) 
     Insert(root->left, word); 
    else 
     Insert(root->right, word); 
} 

TreeType::~TreeType() { 
    Destroy(root); 
} 

int main(int argc, const char * argv[]) { 
    int totalwords = 0; 
    ifstream file; 
    string word; 
    char c; 
    ofstream fout; 

    ifstream file; 
    string filename; 
    cout << "Enter name of file with text to analyze: "; 
    cin >> filename; 
    fin.open(filename.c_str()); 
    if (fin.fail()) { 
     cout << "Error opening file.\n"; 
     exit(1); 
    } 
    cout << "\nAnalyzing " << filename << ".\n"; 

    TreeType t; 

    while(!file.eof()){ 
     file.get(c); 

     if(isalpha(c) || c == '\''){ 
      word += c; 
      c = '\0'; 
     }else if(c == ' ' || c == '\n' || c == '-' || c == '.'){ 
      if(isalpha(word[0])){ 
       transform(word.begin(), word.end(), word.begin(), ::tolower); 
       t.PutItem(word); 
       totalwords++; 
       word = ""; 
       c = '\0'; 
      } 
     } 

    }if(isalpha(word[0])){ 
     transform(word.begin(), word.end(), word.begin(), ::tolower); 
     t.PutItem(word); 
     totalwords++; 
    } 

    file.close(); 
    fout.open("results.txt"); 
    cout << "\nWord counts:\n\n"; 
    t.Print(); 
    t.Print2(fout); 
    cout << "\nTotal number of words in text: " << totalwords << ".\n"; 
    fout << "\nTotal number of words in text: " << totalwords << ".\n"; 
    cout << "Number of distinct words appearing in text: " 
     << distinctWord << ".\n"; 
    fout << "Number of distinct words appearing in text: " 
     << distinctWord << ".\n"; 
    fout.close(); 
    return 0; 
} 
+0

所有呼叫者什麼是你的輸入。你的預期產出是多少?你得到什麼輸出? – bashrc

+0

這是很多代碼。其中大部分可能與您的問題無關。請嘗試創建一個可以向我們顯示的[** Minimal **,Complete和Verifiable示例](http://stackoverflow.com/help/mcve)。 –

回答

2

你將有你的輸出文件流傳遞到您的 PrintTree(root);了。

現在您正在打印到cout,它將轉儲所有應用程序與您的應用程序關聯的任何控制檯。

添加ofstream參數後,修改函數變爲:

void TreeType::PrintTree(Node* root, ofstream &fout) { 
    if (root == nullptr) 
     return; 
    PrintTree(root->left, fout); 
    fout << root->word << " " << root->count << endl; 
    PrintTree(root->right, fout); 
} 

您將需要通過ofstream對象遍及的PrintTree

+0

我愛你的男人。 – Ammar