【问题标题】:Why can't it open the file?为什么打不开文件?
【发布时间】:2015-03-19 14:37:37
【问题描述】:

我的代码有问题。当我运行它时,每次都会弹出我的错误消息说它找不到指定的文件。我错过了什么吗?我正在尝试获取 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();


    }

【问题讨论】:

  • 程序和文件在同一个目录吗?
  • 您的程序逻辑似乎有问题。您首先打开并读取文件,然后检查您是否给出了文件名,最后检查文件是否可以打开。这样做的目的是什么?
  • 你如何称呼你的程序?你给它什么论据?是你说的那个文件吗?
  • 文件在哪里,你给程序的路径是什么?
  • 你不能把你的代码减少到只有相关的部分吗?例如,您不必在已经打开失败时显示您的读/写方式

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2021-09-01
    • 2015-07-06
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多