为什么打不开文件?
Why can't it open the file?
我的代码有问题。当我 运行 它时,每次都会弹出我的错误消息,说它找不到指定的文件。我错过了什么吗?我正在尝试获取 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 <set>
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[])
{
// open the file to be read in
ifstream inFile(argv[1]);
char ch = 0;
while (inFile.get(ch))
{
cout.put(ch);
}
string title = argv[1];
for (unsigned x = 0; x < title.length(); ++x)
{
if (title[x] == '.')
{
title = title.substr(0, x);
break;
}
}
cout << title << endl;
// get the filename from the command line
if (argc <= 1) // if there are no arguments
{
cout << "Error: incorrect number of command line arguments\n"
"Usage: allwords filename" << endl;
return EXIT_FAILURE;
}
//Error checking
if (!inFile)
{
cerr << "Error: failed to open " << " The Republic by, Plato " << endl
<< "Check filename, path, or it doesn't exist.\n";
return EXIT_FAILURE;
}
ofstream outFile("The Republic, by Plato.html");
outFile << "<html xmlns=\"http://www.w3.org/1999//xhtml\"xml:lang=\"en\">" << endl;
outFile << "<head>" << endl;
outFile << "<meta http - equiv = \"Content-Type\" content = \"text/html; charset=UTF-8\" />" << endl;
outFile << "<title>" << "The Republic, by Plato "<< "</title>" << endl;
outFile << "</head>" << endl;
outFile << "<body>" << endl;
// extracting the words from the file and storing it in a container
/*typedef set<string, unsigned> dictionary_type;
dictionary_type words;*/
//infile.clear(); // Clear its state. Otherwise infile.eof() is true
//infile.seekg(0); // rewinds the files contents to be read again starting at position 0
// read the information in to find only words
outFile.open(title);
string word;
while (inFile)
{
if (inFile)
{
getline(inFile, word);
outFile << word << endl;
}
}
//print out the container
//for (auto w : words)
//cout << w << endl;
outFile << "</body>" << endl << "</html>";
// close the file when finished
inFile.close();
}
将以下内容移至main
函数的开头
// get the filename from the command line
if (argc <= 1) // if there are no arguments
{
cout << "Error: incorrect number of command line arguments\n"
"Usage: allwords filename" << endl;
return EXIT_FAILURE;
}
//Error checking
if (!inFile)
{
cerr << "Error: failed to open " << " The Republic by, Plato " << endl
<< "Check filename, path, or it doesn't exist.\n";
return EXIT_FAILURE;
}
我的代码有问题。当我 运行 它时,每次都会弹出我的错误消息,说它找不到指定的文件。我错过了什么吗?我正在尝试获取 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 <set>
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[])
{
// open the file to be read in
ifstream inFile(argv[1]);
char ch = 0;
while (inFile.get(ch))
{
cout.put(ch);
}
string title = argv[1];
for (unsigned x = 0; x < title.length(); ++x)
{
if (title[x] == '.')
{
title = title.substr(0, x);
break;
}
}
cout << title << endl;
// get the filename from the command line
if (argc <= 1) // if there are no arguments
{
cout << "Error: incorrect number of command line arguments\n"
"Usage: allwords filename" << endl;
return EXIT_FAILURE;
}
//Error checking
if (!inFile)
{
cerr << "Error: failed to open " << " The Republic by, Plato " << endl
<< "Check filename, path, or it doesn't exist.\n";
return EXIT_FAILURE;
}
ofstream outFile("The Republic, by Plato.html");
outFile << "<html xmlns=\"http://www.w3.org/1999//xhtml\"xml:lang=\"en\">" << endl;
outFile << "<head>" << endl;
outFile << "<meta http - equiv = \"Content-Type\" content = \"text/html; charset=UTF-8\" />" << endl;
outFile << "<title>" << "The Republic, by Plato "<< "</title>" << endl;
outFile << "</head>" << endl;
outFile << "<body>" << endl;
// extracting the words from the file and storing it in a container
/*typedef set<string, unsigned> dictionary_type;
dictionary_type words;*/
//infile.clear(); // Clear its state. Otherwise infile.eof() is true
//infile.seekg(0); // rewinds the files contents to be read again starting at position 0
// read the information in to find only words
outFile.open(title);
string word;
while (inFile)
{
if (inFile)
{
getline(inFile, word);
outFile << word << endl;
}
}
//print out the container
//for (auto w : words)
//cout << w << endl;
outFile << "</body>" << endl << "</html>";
// close the file when finished
inFile.close();
}
将以下内容移至main
函数的开头
// get the filename from the command line
if (argc <= 1) // if there are no arguments
{
cout << "Error: incorrect number of command line arguments\n"
"Usage: allwords filename" << endl;
return EXIT_FAILURE;
}
//Error checking
if (!inFile)
{
cerr << "Error: failed to open " << " The Republic by, Plato " << endl
<< "Check filename, path, or it doesn't exist.\n";
return EXIT_FAILURE;
}