我正在寻找一种方法来编写一个输入循环,该循环会继续打印提示,直到用户输入一个空行。
这就是我想要做的:
循环开始,打印提示 >
到命令行。用户输入以 '\n' 结尾的行,我的程序对该行执行任何操作,然后再次打印 >
。这一直持续到用户输入一个空行 (\n
),此时循环终止。
最佳答案
类似的东西能满足您的需求吗?
int ret;
// Basically, in order, 1 to indicate the file descriptor of the standard output
// ">" as the string you want to print
// 1 as the number of characters you want printed.
write(1, ">", 1);
while ((ret = read(0, buff, SizeBuff)) > 0) {
buff[ret] = 0;
if (buff[0] == '\n' && strlen(buff) == 1)
return (0);
/*Do your stuff*/
write(1, ">", 1);
}
关于c - 如何创建输入循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249927/