为什么不能打开文件?

时间:2015-03-19 14:37:37

标签: c++ command-line-arguments iostream fstream

我的代码有问题。当我运行它时,每次说它无法找到指定的文件时会弹出我的错误消息。我错过了什么?我试图获取txt文件的常规内容并将其转换为html文件,以便我可以在Web浏览器中查看它。我知道我错过了一些我不知道它是什么的东西。任何人都可以解释为什么它不会找到该文件。提前致谢。

//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();


    }

1 个答案:

答案 0 :(得分:0)

将以下内容移至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;
    }
相关问题