为什么它只打印文本文件的一个词而不是将整个文本文件打印到 html 文件
Why is it only printing one word of the text file rather than the whole text file to a html file
我目前正在开发一个项目,该项目使用命令行属性将 .txt 文件转换为 .xhtml 文件。特别地,该程序将 ASCII 文本文件转换为 xhtml 1.0 文件,其中包含与原始 ASCII 文本文件相同的文本内容。我似乎遇到的问题是,当我打开 .html 文件以从旧的 .txt 文件中读取内容时,只有文件中的一个词被读入 html 文件。谁能解释为什么会这样?对你的帮助会很大 appreciated.Thank 提前。
//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>";
}
我看到几个问题:
您正在先读取文件的内容,将内容回显到 std::cout
。完成之后,就没有什么可以从文件中读取的了。添加调用以倒回文件,然后再次读取其内容。
infile.clear(); // Clear its state. Otherwise infile.eof() is true.
infile.seekg(0); // rewind
这些行需要在
之前
while (getWord(infile, word))
您有以下行:
if (is)
swap(word, buffer); // C++98(swap owner, then destory the old)
word = std::move(buffer); // C++ 11
您只需要使用其中一个,不需要同时使用两个。如果同时使用两者,word
将设置为空字符串。
我目前正在开发一个项目,该项目使用命令行属性将 .txt 文件转换为 .xhtml 文件。特别地,该程序将 ASCII 文本文件转换为 xhtml 1.0 文件,其中包含与原始 ASCII 文本文件相同的文本内容。我似乎遇到的问题是,当我打开 .html 文件以从旧的 .txt 文件中读取内容时,只有文件中的一个词被读入 html 文件。谁能解释为什么会这样?对你的帮助会很大 appreciated.Thank 提前。
//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>";
}
我看到几个问题:
您正在先读取文件的内容,将内容回显到
std::cout
。完成之后,就没有什么可以从文件中读取的了。添加调用以倒回文件,然后再次读取其内容。infile.clear(); // Clear its state. Otherwise infile.eof() is true. infile.seekg(0); // rewind
这些行需要在
之前 while (getWord(infile, word))
您有以下行:
if (is) swap(word, buffer); // C++98(swap owner, then destory the old) word = std::move(buffer); // C++ 11
您只需要使用其中一个,不需要同时使用两个。如果同时使用两者,
word
将设置为空字符串。