c - 如何创建输入循环?

标签 c loops input

我正在寻找一种方法来编写一个输入循环,该循环会继续打印提示,直到用户输入一个空行。

这就是我想要做的:

循环开始,打印提示 > 到命令行。用户输入以 '\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/

相关文章:

scala - 处理 Set of Sets 并返回一个扁平的 Iterable

c++ - Code::blocks,定义默认输入

ruby - 简单的 Ruby 输入验证库

c - 为什么消息队列没有类似轮询/选择的机制?

arrays - 如何丢弃用户输入中除 a-z && A-Z && 0-9 之外的所有字符以查找可能的回文

C:分配给指针时数组的行为

python - 为 Pandas 中的每个唯一值创建一个数据框

arrays - 如何动态获取变量名

python - 在pygame中使用文本输入

c - "Array"功能正在阻止程序正常运行