简体   繁体   English

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

[英]How can i detect just the new line input using scanf, and printing directory, like terminal

I wanna reproduce the terminal behavior when the input is just a new line (keeps printing the same string), but don't know how to do it.当输入只是一个新行(继续打印相同的字符串)时,我想重现终端行为,但不知道该怎么做。

Example: When the user just inputs a new line, the terminal keeps printing the directory, until a real command is inserted示例:当用户刚输入新行时,终端一直打印目录,直到插入真正的命令

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

At the first enter press, it enters the loop, reproduce 1 time, and then the scanf doesn't detect new lines anymore, it just skips and waits to a real string.在第一次enter按下时,它进入循环,重现 1 次,然后scanf不再检测新行,它只是跳过并等待一个真正的字符串。 What can i type inside of the scanf to detect a new line input and keep printing that string till a real command is inserted?我可以在scanf中输入什么来检测新行输入并继续打印该字符串直到插入真正的命令?

I tried with scanf("%c"...) but the problem with a char, is that i can't process the whole string command, if isn't empty我尝试使用scanf("%c"...)但字符的问题是,如果不为空,我无法处理整个字符串命令

First of all, your two scanf calls are different.首先,你的两次scanf调用是不同的。 The first one is第一个是

scanf("%[^\n]", userInput);

which looks for anything that's not a newline, as you wish to do.它会按照您的意愿查找任何不是换行符的内容。

But the second one is但第二个是

scanf(" %[^\n ]", userInput);

which is also looking for a space before the input, followed by any character that is also not a newline or a space .它也在输入之前寻找一个空格,然后是任何不是换行符或空格的字符。 Thus, scanf is waiting for the space.因此, scanf 正在等待空间。


IMHO, the best way to recreate this behavior is going to be in the parsing step, after you have gotten the command from the command line.恕我直言,重新创建此行为的最佳方法是在解析步骤中,在您从命令行获取命令后。 Essentially, your command input loop would look like this:本质上,您的命令输入循环如下所示:

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

(Note the use of POSIX getline() instead of scanf . This is a more recent standard library function that does exactly the task of getting a line of user input, and also allocates the buffer using malloc and realloc so that you don't have to care about buffer overflows or even sizing the buffer at all.) (注意使用 POSIX getline()而不是scanf 。这是一个更新的标准库函数,它完全完成了获取用户输入行的任务,并且还使用mallocrealloc分配缓冲区,这样你就没有关心缓冲区溢出甚至调整缓冲区大小。)

The user input function wouldn't care that the userInput portion was blank.用户输入函数不会关心userInput部分是否为空。 The function that would care is the parse function, which will simply interpret a blank userInput string as "do nothing" and continue on its merry way.需要关心的函数是parse函数,它会简单地将一个空白的userInput字符串解释为“什么都不做”,然后继续它的快乐方式。

Hmm, the code I gave pretty much does that with one exception, it doesn't display a prompt each time...嗯,我给出的代码几乎可以做到这一点,只有一个例外,它每次都不会显示提示......

Is this what you mean:你是这个意思吗:

#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");
    }

}

I changed the scanf() to fgets() to read from stdin so that we don't overwrite the buffer.我将scanf()更改为fgets()以从stdin读取,以便我们不会覆盖缓冲区。

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

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