【问题标题】:How to input a file into C++ and comparing console input如何将文件输入 C++ 并比较控制台输入
【发布时间】:2012-09-07 18:48:09
【问题描述】:

我正在做我的家庭作业,但我卡住了,因为在作业中我们必须要求用户输入文件名,还要输入 wc cc 或 lc(文件的字数、字符数和行数.例如,wc filename.txt。我想检查文件以查看它是否有效,我理解并且我知道如何比较用户输入以确定要运行的不同类型的函数,但我不明白你是如何可以一起做。有什么想法吗?这就是我目前的想法。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
string line;
string file;

ifstream input; //input file stream
int i;

cout << "Enter a file name" << endl;

while(true){

    cout << ">" ;
    getline(cin,file);

    input.open(file.c_str());

     if (input.fail()) {
        cerr << "ERROR: Failed to open file " << file << endl;
        input.clear();
     }
     else {
        i = 0;
        while (getline(input, line))

            if(line == "wc"){

            cout << "The word count is: " << endl;
    }
        else if(line == "cc"){

            cout << "The character count is: " << endl;
    }
        else if(line == "lc"){

            cout << "The line count is: " << endl;
    }
        else if(line == "exit"){

            return 0;
    }
        else{

            cout << "----NOTE----" << endl;
            cout << "Available Commands: " << endl;
            cout <<"lc \"filename\"" << endl;
            cout <<"cc \"filename\"" << endl;
            cout <<"wc \"filename\"" << endl;
            cout <<"exit" << endl;
    }



     }



}


return 0;
}

void wordCount(){
   //TBD
}

void characterCount(){
    //TBD

}

void lineCount(){
  //TBD

}

【问题讨论】:

  • 我不确定你的问题是什么问题,当你一起说时你是什么意思。您显然首先需要检查文件,如果没有问题,请执行操作。
  • Stack Overflow 是一个期望答案能够持续并对其他人有所帮助的地方,因此在回答问题时不应删除问题。一旦有人以令人满意的方式回答了您的问题,您通常会单击帖子分数下方的复选标记(答案左侧);这将问题标记为“已回答”。我已将您的问题文本恢复到您将其全部删除之前,如果您自己找到了解决方案,或者接受下面对您有帮助的一个答案,我鼓励您发布您的解决方案。

标签: c++ file input fstream


【解决方案1】:

您必须在用户输入中找到命令和文件名之间的空格,然后在找到空格的位置拆分字符串。像这样的

cout << "Enter a command\n";
string line;
getline(cin, line);
// get the position of the space as an index
size_t space_pos = line.find(' ');
if (space_pos == string::npos)
{
   // user didn't enter a space, so error message and exit
   cout << "illegal command\n";
   exit(1);
}
// split the string at the first space
string cmd = line.substr(0, space_pos);
string file_name = line.substr(space_pos + 1);

这是未经测试的代码。

您可以做得比这更好,例如,如果用户在命令和文件名之间输入两个空格,这将不起作用。但是这种工作很快就会变得非常乏味。由于这是一项任务,我很想继续做更有趣的事情。如果有时间,您可以随时回来改进。

【讨论】:

    【解决方案2】:

    我想你是在问如何验证多个参数:命令和文件。

    一个简单的策略是具有如下功能:

    #include <fstream> // Note: this is for ifstream below
    
    bool argumentsInvalid(const string& command, const string & command) {
        // Validate the command
        // Note: Not ideal, just being short for demo
        if("wc" != command && "cc" != command && "lc" != command) {
            std::cout << "Invalid command" << std::endl;
            return false;
        }
    
        // Validate the file
        // Note: This is a cheat that uses the fact that if its valid, its open.
        std::ifstream fileToRead(filename);
        if(!fileToRead) {
            std::cout << "Invalid file: \"" << filename << "\"" << std::endl;
            return false;
        }
    
        return true;
        // Note: This does rely on the ifstream destructor closing the file and would mean
        // opening the file twice.  Simple to show here, but not ideal real code.
    }
    

    如果您想在返回错误之前评估所有参数,请在该函数的顶部插入一个标志,例如:

    // To be set true if there is an error
    bool errorFound = false;
    

    并将条件中的所有返回更改为:

    errorFound = true;
    

    最后返回:

    return !errorFound;
    

    用法:

    ....
    
    if(argumentsInvalid(command, filename)) {
        std::cout << "Could not perform command.  Skipping..." << std::endl;
        // exit or continue or whatever
    }
    
    // Now do your work
    

    注意:这里的具体有效性测试过于简化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 2023-03-16
      相关资源
      最近更新 更多