为什么它只打印文本文件的一个单词,而不是将整个文本文件打印到html文件中

Why is it only printing one word of the text file rather than the whole text file to a html file

本文关键字:文件 文本 打印 html 一个 为什么 单词      更新时间:2023-10-16

我目前正在处理一个项目,该项目使用命令行属性将.txt文件转换为.xhtml文件。特别地,该程序将ASCII文本文件转换为xhtml 1.0文件,该文件包含与原始ASCII文本文件相同的文本内容。我似乎遇到的问题是,当我打开.html文件读取旧.txt文件中的内容时,该文件中只有一个单词被读取到html文件中。有人能解释为什么会这样吗?非常感谢您的帮助。提前谢谢。

//Programmer:
    //Date: March 9 2015
    //Purpose: converts an old style text file into any format
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <map>
    using namespace std;
    // getWord function to read in all words
    istream& getWord(istream& is, string& word)
    {
        // find the beginning of the word (ie . eat all the non alphas)
        char ch;
        while (is.get(ch))
        {
            if (isalpha(ch))
                break;
        }
        // quit if no word found
        if (!is)
            return is;
        string buffer;
        buffer += ch;   // put the valid alpha onto the buffer
        while (is.get(ch))
        {
            if (isalpha(ch))
                buffer += ch;
            else
                break;
        }
        if (is)
            is.unget();
        if (is.eof())
            is.clear();
        if (is)
            //word = buffer;        // put the complete buffer into the word so it can be returned by reference. 
            //This does a copy + destroy!!
            swap(word, buffer);         // C++98(swap owner, then destory the old)
        word = std::move(buffer);   // C++ 11 
        return is;
    }
    int main(int argc, char* argv[])
    {
        ifstream infile(argv[1]);
        char ch = 0;
        while (infile.get(ch)){
            cout.put(ch);
        }
        // print out all the command line arguments
        for (size_t i = 0; i < argc; ++i)
        {
            string s = (string)argv[i];
            cout << s << endl;
        }
        //if input file is at location 1 in the command line
        string input = argv[1];
        for (size_t i = 0; i < input.size(); ++i)
        {
            cout.put(input[i]);
        }
        cout << endl;

    // Creating the html output file
        ofstream out("title.html");
        out << "<html xmlns="http://www.w3.org/1999//xhtml"xml:lang="en">" << endl;
        out << "<head>" << endl;
        out << "<meta http - equiv = "Content-Type" content = "text/html; charset=UTF-8" />" << endl;
        out << "<title>" << argv[1] << "</title>" << endl;
        out << "</head>" << endl;
        out << "<body>" << argv[1] << endl;
        // extracting the words from the file and storing it in a container
        typedef map<string, unsigned> dictionary_type;
        dictionary_type words;
        // read the information in to find only words
        string word;
        while (getWord(infile, word))
        {
            auto loc = words.find(word);
            if (loc == words.end())
                words.insert(pair<string, int>(word, 1));
            else
                loc->second++;
        }
        //print out the container
        for (auto w : words)
            cout << w.first << ": " << w.second << endl;
        out << "</body>" << endl << "</html>";

    }

我看到了几个问题:

  1. 您首先读取文件的内容,将内容回显到std::cout。完成后,就没有什么可从文件中读取的了。添加一个调用以倒带文件,然后再次读取其内容。

    infile.clear();  // Clear its state. Otherwise infile.eof() is true.
    infile.seekg(0); // rewind
    

这些线路需要在之前

    while (getWord(infile, word))
  1. 你有行:

    if (is)
       swap(word, buffer);         // C++98(swap owner, then destory the old)
    word = std::move(buffer);   // C++ 11 
    

    您只需要使用其中一个,而不需要同时使用两个。如果同时使用两者,则word将设置为一个空字符串。