Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用scanf和打印目录(如终端)检测新行输入_C - Fatal编程技术网

如何使用scanf和打印目录(如终端)检测新行输入

如何使用scanf和打印目录(如终端)检测新行输入,c,C,当输入只是一个新行(一直打印相同的字符串)时,我想重现终端行为,但不知道如何做 示例:当用户刚刚输入新行时,终端会一直打印目录,直到插入真正的命令 int main() { char userInput[1024]; while (1) { printf("directory »» "); scanf("%[^\n]" , userInput); // This scanf doesn't work while (userI

当输入只是一个新行(一直打印相同的字符串)时,我想重现终端行为,但不知道如何做

示例:当用户刚刚输入新行时,终端会一直打印目录,直到插入真正的命令

int main()
{
    char userInput[1024];
    while (1)
    {
        printf("directory »» ");
        scanf("%[^\n]" , userInput); // This scanf doesn't work
        while (userInput[0] == '\n')  // If the input is only a new line char, keep asking for more inputs and printing the directory
        {
            printf("directory »» ");
            scanf(" %[^\n ]" , userInput); // This scanf doesn't work
        }

        //Input isn't a NewLine, process the input
        process_Input_Function(userInput); //Isn't empty, search for my created commands
    }
}
在第一次按下
enter
时,它进入循环,复制1次,然后
scanf
不再检测新行,它只是跳过并等待一个真正的字符串。 我可以在
scanf
中键入什么来检测新行输入并一直打印该字符串,直到插入真正的命令


我尝试了
scanf(“%c”…)
但是char的问题是,我无法处理整个string命令,如果它不是空的

首先,您的两个
scanf
调用是不同的。第一个是

scanf("%[^\n]", userInput);
它寻找任何不是新行的东西,就像你想做的那样

但第二个是

scanf(" %[^\n ]", userInput);
也就是在输入前寻找一个空格,后跟任何字符,也不是换行符或空格。因此,scanf正在等待空间


IMHO,重新创建此行为的最佳方法是在从命令行获得命令后,在解析步骤中。基本上,您的命令输入循环如下所示:

char *userInput = NULL;
size_t n = 0;
while (true) {
    // print the prompt
    printf(">");
    // get the line
    ssize_t userInputLength = getline(&userInput, &n, &stdin); 
    // parse the input, using a function you wrote elsewhere
    parse(userInputLength, userInput);
}
(请注意使用POSIX而不是
scanf
。这是一个较新的标准库函数,它完全执行获取一行用户输入的任务,并且还使用
malloc
realloc
分配缓冲区,这样您就不必关心缓冲区溢出,甚至不必调整缓冲区大小。)


用户输入函数不关心
userInput
部分是否为空。需要关心的函数是
parse
函数,它将把一个空白的
userInput
字符串解释为“不做任何事情”,然后继续它的快乐方式

嗯,我给出的代码基本上就是这样的,只有一个例外,它不会每次都显示提示

这就是你的意思吗:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // For the memset()

int main() {

    char userInput[1024];

    while (1) {
        printf("»»» ");

        fgets(userInput, 1024, stdin);

        while (userInput[0] == '\n')
        {
            printf(">>> ");
            memset(userInput, '\0', 1024);
            fgets(userInput, 1024, stdin);
        }

        // Your command can be accessed from here //
        printf("Command entered: %s\n", userInput);

        printf("Input isn't a NewLine\n");
    }

}
#包括
#包括
#包含//用于memset()的
int main(){
字符用户输入[1024];
而(1){
printf(“»»”);
fgets(用户输入,1024,标准输入);
while(用户输入[0]='\n')
{
printf(“>>>”);
memset(用户输入,'\0',1024);
fgets(用户输入,1024,标准输入);
}
//您的命令可以从这里访问//
printf(“输入的命令:%s\n”,用户输入);
printf(“输入不是换行符\n”);
}
}

我将
scanf()。至少使用1024,而不是10。您也可以使用<代码> GETC< <代码>或<代码> Read <代码>。考虑不使用SCANF代码。代码不能使用<代码> SCANF(“%s”…)/代码>读取一行。使用<代码> SCANFE()/String转换(<代码> %S/<代码>或<代码> %[] /代码>),没有字段宽度(或使用“代码> *>代码>的赋值抑制”)是危险的,因为用户提供的输入可能会使缓冲区溢出。请尝试使其与第一个完全相同。行吗?@marcospb19好的,有道理。我只是在我的答案中添加了一些建议,以建议您如何真正实现这种行为。我理解,但似乎这也创建了一个无限循环(后面的空格通常会阻止这种循环,但我没有找到修复代码的方法)。第二个
“\n”
scanf中(“%[^\n]\n”,userInput)
导致
scanf()
使用所有空白,包括
'\n'
和所有其他后续WS,直到输入非空白。请注意。