程序运行,但文件无法打开.C++中的命令行

Program runs but file will not open. Command line in C++

本文关键字:C++ 命令行 运行 文件 程序      更新时间:2023-10-16

当我输入出现的用户提示时,我不确定为什么。我输入"cat"或"File.txt",它不会打开。我知道我做错了什么,这是我第一次在C++中使用命令行参数。所以要么我的代码是错误的,要么文件不在它需要的地方。我把它和.cpp文件放在一起,所以我认为它在正确的地方。File.txt只是一个带有 3 行代码的 txt 文件,当用户输入命令时,我需要输出到屏幕上。

我还有一个头文件,其中包含一个类和头文件中所需的所有内容。

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;
}

从使用

split(cmd, args, ' ');

您需要使用std::getline阅读一行文本cmd.用途

cin >> cmd;

不合适。一旦在流中遇到空格字符,它将停止读取。将该行替换为

getline(cin, cmd);