简体   繁体   English

C ++中的命令提示符

[英]command prompt in c++

Guys what am I doing wrong here? 伙计们,我在这里做错了什么?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    while (true){
        std::string cmd;
        cin >> cmd;

        const char* com = cmd.c_str();

        cout << com << endl;
       // cout << sizeof(com) << endl;

        system(com);
    }
    return 0;
}

Everytime I run this it works fine but when you type something like cd ../ it separates the words and runs them as two different commands so first cd, then ../ and it gives me a error. 每次我运行它时,它都运行良好,但是当您键入cd ../之类的东西时,它会将单词分开并以两个不同的命令运行它们,所以先输入cd,然后是../,这给我一个错误。 Any idea on what I'm doing wrong? 关于我在做什么错的任何想法吗? I'm new to C++ anyway also this is supposed to bypass "command prompt has been disabled by your admin on windows" 无论如何,我还是C ++的新手,这应该绕过“您的Windows管理员已禁用命令提示符”

the extraction operator (>>) stops reading when reaching the first white-space if your command consists of spaces then use std::getline: 如果您的命令由空格组成,则提取运算符(>>)会在到达第一个空格时停止读取,然后使用std :: getline:

std::string sCommand;
std::cout << "Enter eommand: ";
std::getline(std::cin, sCommand); // eg enter: color 1f
system(sCommand.c_str()); // c_str(): converts from class string to const char*.

there's no way to pass two arguments one after the other to system when the first arguments invokes a program and the second is passed to it but you can make on big command then pass it. 当第一个参数调用程序并将第二个参数传递给程序时,无法将两个参数一个接一个地传递给系统,但是您可以执行大命令然后将其传递。

system("diskpart"); // invoking diskpart
system("list vol"); // here list vol is not passed to diskpart but only to cmd  

system("notepad.exe C:/desktop/mylog.txt"); // ok

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM