簡體   English   中英

為什么無法打開文件?

[英]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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM