如何将文件输入到C++并比较控制台输入
How to input a file into C++ and comparing console input
我正在做家庭作业,但我坚持了下来,因为在作业中,我们必须要求用户输入文件名,同时还要键入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
}
您必须在用户输入中找到命令和文件名之间的空格,然后在找到空格的位置拆分字符串。像这样的
cout << "Enter a commandn";
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 commandn";
exit(1);
}
// split the string at the first space
string cmd = line.substr(0, space_pos);
string file_name = line.substr(space_pos + 1);
这是未经测试的代码。
您可以做得更好,例如,如果用户在命令和文件名之间输入两个空格,这将不起作用。但这种工作很快就会变得非常乏味。由于这是一项任务,我很想继续做一些更有趣的事情。如果你有时间的话,你可以随时回来改进。
我想您正在询问如何验证多个参数:命令和文件。
一个简单的策略是具有如下功能:
#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
注意:这里的具体有效性测试过于简化。
相关文章:
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 使用-L和-L标志与将库文件作为输入进行比较
- 通过与输入进行比较来安排输出
- 将私有对象变量与用户输入的变量进行比较
- 输入迭代器的相等性比较
- 将用户输入的术语与 txt 文件中的列表进行比较
- 将 ProcessEntry32.szExeFile 与用户输入的数据进行比较时,C++ _wcsicmp代码未编译
- 如何使用文件填充数组并将其与用户输入C 进行比较
- 从类中查看 rand() 并将用户输入与计算机选择进行比较.石头,纸,剪刀游戏
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 在比较特定输入中的程序的数组中出错
- 将最大最小值的数据与可变数量的输入进行比较 C++
- c++ 输入一个数字来确定其他数字,然后比较它们
- 比较输入的C弦与文件读取c-string
- 将用户输入与矢量中存储的值进行比较
- C++ - 读取和比较用户输入
- 将结果与用户输入-SQLITE3 C 进行比较
- 正在检查用户的有效输入并与文本文件进行比较
- C++如何将用户输入与具有严格排序的字符串列表进行比较