Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用打印功能输出到新文件_C++_Ofstream - Fatal编程技术网

C++ 使用打印功能输出到新文件

C++ 使用打印功能输出到新文件,c++,ofstream,C++,Ofstream,我有一个程序,分析一个文本文件中不同的总字数,然后将其写入一个新的输出文件。我已经记下了第一部分,但我不知道如何让打印函数打印到新的文本文件上。在新文件中打印全部单词或不同的单词是可行的,但Print2()函数似乎不起作用。请看一下//兴趣点//部分,我认为问题就出在那里 #include <iostream> #include <fstream> #include <string> #include <cctype> int distinctWo

我有一个程序,分析一个文本文件中不同的总字数,然后将其写入一个新的输出文件。我已经记下了第一部分,但我不知道如何让打印函数打印到新的文本文件上。在新文件中打印全部单词或不同的单词是可行的,但Print2()函数似乎不起作用。请看一下//兴趣点//部分,我认为问题就出在那里

#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;
}
#包括
#包括
#包括
#包括
int distinctWord=0;
使用名称空间std;
typedef int ItemType;
结构节点{
字符串字;
整数计数=1;
节点*左;
节点*右;
};
类树型{
私人:
节点*根;
无效插入(节点*&根,字符串字);
无效销毁(节点*&根节点);
作废打印树(节点*根);
void Print2File(节点*根,流和流的根);
公众:
TreeType(){root=nullptr;}
~TreeType();
无效项(字符串字);
作废打印();
无效打印2(流和流);
};
void TreeType::Print(){
打印树(根);
不能离开);
不能留下单词);
左四字=nullptr;
返回;
}
如果(根->单词==单词){
根->计数++;
返回;
}else if(wordword)
插入(根->左,单词);
其他的
插入(根->右,单词);
}
TreeType::~TreeType(){
消灭(根);
}
int main(int argc,const char*argv[]{
整数totalwords=0;
ifstream文件;
字符串字;
字符c;
流式流量计;
ifstream文件;
字符串文件名;
cout>文件名;
打开(filename.c_str());
if(fin.fail()){

cout您必须将输出文件流传递给
打印树(根);
也是

现在,您正在打印到
cout
,它将把所有内容转储到与您的应用程序关联的任何控制台

添加流的
参数后,修改后的函数变为:

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);
}
void TreeType::PrintTree(节点*根,流和fout的根){
if(root==nullptr)
返回;
打印树(根->左,右);

fout word您必须将输出文件流传递给
打印树(根);
也是

现在,您正在打印到
cout
,它将把所有内容转储到与您的应用程序关联的任何控制台

添加流的
参数后,修改后的函数变为:

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);
}
void TreeType::PrintTree(节点*根,流和fout的根){
if(root==nullptr)
返回;
打印树(根->左,右);

总之,你的输入是什么。你的预期输出是什么。你得到了什么输出?那是很多代码。其中大部分可能与你的问题无关。请尝试创建一个你可以向我们展示的。你的输入是什么。你的预期输出是什么。你得到了什么输出?那是很多代码。其中大部分可能与你的问题无关您的问题。请尝试创建一个可以显示给我们的。