C++ C++;循环I/O问题

C++ C++;循环I/O问题,c++,io,C++,Io,使用std::getline从流中获取字符串,而不是std::cin>>命令 Enter command (with arguments) to profile: ls Running command: ls Enter command (with arguments) to profile: homework8 Homework8.cpp~ Process.cpp~ stats.dat Homework8 plot_stats.gnu Process.h

使用std::getline从流中获取字符串,而不是std::cin>>命令

Enter command (with arguments) to profile: ls
Running command: ls
Enter command (with arguments) to profile: homework8      Homework8.cpp~  Process.cpp~    stats.dat
Homework8      plot_stats.gnu  Process.h       TestProgram.cpp
Homework8.cpp  Process.cpp     ProcessStats.h
或者需要在从流获取之前检查std::cin

std::string command;
std::getline(std::cin, command);
使用std::cin;
使用std::getline;
字符行[最大行];/*行缓冲区*/
while(true)
{
/*获得命令*/
cout
intmain(intargc,char*argv[]){
while(true){
std::cout>命令;

std::cout使用
std::getline
获得整行。执行
std::cin>>command;
会在第一个空格处停止,因此如果输入带参数的命令,则会对该行进行多次循环

int main(int argc, char *argv[]) {
    while (true) {
        std::cout << "Enter command (with arguments) to profile: ";
        std::string command;
        std::cin >> command;
        std::cout << "Running command: " << command << std::endl;
        Process::create(true, command);
        std::cin.clear();
        std::cin.sync();
    }
}
while(std::getline(std::cin,命令)
{

std::cout它仍在等待您输入下一个命令。您可能希望在接受下一个命令之前等待进程完成,对吗?在切换到使用std::cin>>之前,我正在这样做,但它也不起作用。请尝试:使用std::cin;使用std::getline;字符行[MAX_line];/*行缓冲区/while(true){/Get command*/cout似乎仍然在给我造成同样的问题。
using std::cin;
using std::getline;

char line[MAX_LINE];    /* Line buffer */

while(true)
{
    /* Get command */
    cout << "viettuan@shell:~$ ";
    if (!cin)
        cin.clear();
    else
        cin.getline(line, MAX_LINE);

    //... Do process here
}
int main(int argc, char *argv[]) {
    while (true) {
        std::cout << "Enter command (with arguments) to profile: ";
        std::string command;
        std::cin >> command;
        std::cout << "Running command: " << command << std::endl;
        Process::create(true, command);
        std::cin.clear();
        std::cin.sync();
    }
}
while ( std::getline(std::cin, command ) 
{
    std::cout << "Running command: " << command << std::endl;
    Process::create(true, command);
}

// getting here means they closed stdin