程序运行,但文件无法打开.C++中的命令行
Program runs but file will not open. Command line in C++
当我输入出现的用户提示时,我不确定为什么。我输入"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);
相关文章:
- 从命令行c++发送文本文件名
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 数组长度,为什么从命令行获取时不能使用它?
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 通过 Openssl 命令行加密,通过 c++ 解密
- CMake "--target install"无需"--build"命令行
- 更改命令行 qt5 源代码构建配置的正确/快速方法
- 使用 C++ 将命令行参数拆分为参数/向量
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 如何在OMNET++中添加专门的命令行参数?
- Makefile g++ 使用命令行中的 -D 变量进行编译,默认值
- 在命令行中创建快捷命令
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- C++命令行提示符不应重复
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- 通过命令行在Visual Studio中编译单个独立文件