我不确定为什么输入出现的用户提示。我键入“ cat”或“ File.txt”,它将无法打开。我知道我做错了什么,这是我第一次在C ++中使用命令行参数。因此,要么我的代码错误,要么文件不在所需的位置。我将它与.cpp文件放在一起,所以我认为它在正确的位置。 File.txt只是一个txt文件,其中包含3行代码,当用户输入命令时,我需要将其输出到屏幕上。
我还有一个头文件,其中包含一个类以及头文件中所需的所有内容。
class Cat {
public:
static int main(int argc, std::string argv[]);
};
非常感谢您提供解决此问题的帮助。
//This is Cat.cpp
// Source file for the Cat class
// Implements a command line utility that displays the
// contents of a file.
#include "Cat.hpp"
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int Cat::main(int argc, string argv[]) {
//The main function takes two arguments :
//1. Check if the user provided the correct number of arguments to the command.
//Our version of cat only takes one argument, so argc should be
//2. If not, display the message usage : cat <filename> and return 1 (representing failure.)
//3. Attempt to open the file whose is at argv[1].
//IF the file cannot be opened, display the message error : file not found and return 1.
//Display the contents of the file.
if (argc > 1)
{
cout << "File.txt" << argv[1] << endl;
}
else
{
cout << ": cat <File.txt>. return 1. \n";
return -1;
}
ifstream infile(argv[1]); //open the file
if (infile.is_open() && infile.good())
{
cout << "File is now open!\nContains:\n";
string line = "";
while (getline(infile, line))
{
cout << line << '\n';
}
}
else
{
cout << ": File not found. return 1";
}
infile.close();
return 0;
}
//This is shell.cpp
// This program implements a simple shell
#include <iostream>
#include <string>
#include "Cat.hpp"
#include <vector>
#include <algorithm>
using namespace std;
// Maximum number of arguments (including command) allowed
// in a command line.
const int MAX_ARGS = 16;
int scanCommandLine(string cmd, string argv[]);
size_t split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
size_t pos = txt.find(ch);
size_t initialPos = 0;
strs.clear();
while (pos != std::string::npos) {
strs.push_back(txt.substr(initialPos, pos - initialPos));
initialPos = pos + 1;
pos = txt.find(ch, initialPos);
}
strs.push_back(txt.substr(initialPos, std::min(pos, txt.size()) - initialPos + 1));
return strs.size();
}
int main()
{
//Your shell must repeatedly display a prompt(a $ followed by a space),
//then wait for the user to type in a command line.
//If the user types nothing or only types spaces or tabs, the shell does nothing :
//The Prompt
bool exit = false;
while (!exit)
{
cout << "$ " ;
string cmd;
cin >> cmd;
vector <string> args;
split(cmd, args, ' ');
//If the user types a valid command, the shell should execute the command and display a new prompt :
//If the user types exit, the shell exits :
if (args[0] == "exit")
{
exit = true;
}
else if (args[0] == "cat")
{
string * s = new string[args.size()];
for (int i = 0; i < args.size(); i++)
{
s[i] = args[i];
}
Cat::main(args.size(), s);
}
//If the user types an invalid command, the shell displays an error :
else
{
cout << args[0] << ": Command not found." << endl;
}
}
//The shell will keep repeating this process until the user types exit.
}
int scanCommandLine(string cmd, string argv[])
{
int argc = 0;
size_t start = 0;
size_t end = 0;
string arg;
while(end != string::npos && argc < MAX_ARGS) {
start = cmd.find_first_not_of( " \t", end );
if(start == string::npos)
break;
end = cmd.find_first_of(" \t", start);
argv[argc] = cmd.substr(start, end - start);
argc++;
}
return argc;
}