【发布时间】:2018-11-15 18:49:29
【问题描述】:
我想重现终端行为,如果可能的话使用scanf:
当用户刚刚输入一个新行时,终端会一直打印目录,直到插入一个真正的命令。
插入新行时函数scanf 的正常行为是不断跳空行,等待用户真正插入有效字符。
观察:当我执行 ./main.out 时,此终端模拟正在运行,这意味着我不是在谈论我的操作系统终端,而是在谈论我的程序中的 C 模拟终端。
当您插入空行时,scanf 通常会发生什么:
realPC:~/realDirectory$ ./main.out //starting our simulated terminal
pc:~/Desktop$ '\n'
'\n'
'\n'
"COMMAND\n" //PROCESS THE COMMAND!
pc:~/Desktop$ //waiting...
我想要什么:
realPC:~/realDirectory$ ./main.out //starting our simulated terminal
pc:~/Desktop$ '\n'
pc:~/Desktop$ '\n'
pc:~/Desktop$ '\n'
pc:~/Desktop$ "COMMAND\n" //Process the command here
pc:~/Desktop$ //waiting...
这个目录只是一个例子,我想打印任何消息(例如,在空输入后继续打印箭头">>>"),问题是scanf似乎甚至不考虑'\n'一个输入,所以我不能打印任何东西
我知道fgets(userInput, 1024, stdin) 可以代替scanf,但我想知道是否可以使用scanf 函数(我不习惯fgets,如果可能的话,我也接受其他解决方案建议)
这是代码(scanf 并没有像我假装的那样同时用于两种用途):
int main()
{
char userInput[1024];
char pwd[] = "pc:~/marcospb19"; // Directory that keeps being printed
while (1)
{
printf("%s$ " , pwd); // Print the directory just like terminals do
scanf(" %s" , userInput); // I wanted this to enter the while and keep printing directory
while (userInput[0] == '\n') // Keeps the input inside the loop, if is a new line
{
puts("We're inside this loop! that's good, now keep printing directory...");
printf("%s$ " , pwd); // Directory printed, next: take input again
scanf(" %s" , userInput); // This should be able to receive '\n'.
// This loop should continue if user types '\n'
}
// Now, you can process the input, it isn't empty!
printf("Process the input: %s\n", userInput);
}
}
显然,当我说用户键入'\n' 时,他只是在按回车键(不是真正键入它)。
【问题讨论】:
-
我怀疑
fgets和一些输入按摩会比scanf为您提供更好的服务(无论如何通常都是这样)。 -
您可能会发现
fgets+sscanf比单独使用scanf更容易。
标签: c