在c中使用read()获取输入

发布于 2025-01-10 14:35:59 字数 1563 浏览 2 评论 0原文

所以我尝试使用 read() 获取用户输入。 while循环一直运行,直到在linux终端输入ctrl+d,输入停止。我编写了一些代码并且它有效,但我遇到的问题是我不知道如何准确获取输入中的内容以及如何使用它。

在下面的代码中,就在 if(buff[offset] == '\n') 之后(运行给定的输入,完成后清除缓冲区并移至下一个输入)我不知道不知道如何进入 3 种可能的输入情况,也不知道如何从第二种情况中获取数字:

  • “e”- 退出程序
  • “p N”- 做某事,N 是一个数字,其中我还需要进入一个变量(我认为buff[offset-1] 应该得到数字,但我不太确定。)
  • 其他一切 - 打印一条消息

代码:

int fd = 0; // set read() to read from STDIN_FILENO, because it's number is 0
const size_t read_size = 1; // set chunk size
size_t size = read_size; 
size_t offset = 0;
size_t res = 0;

char *buff = malloc(size+1);
*buff = '\0';

while((res = read(fd, buff + offset, read_size)) > 0) // read from stdin and save to buff
{       
    if(buff[offset] == '\n')
    { // THIS PART
        buff[offset] = '\0';
        if(buff == "e")
        {
            // exit the program
            return 0;
        }
        else if(buff == "p")
        {
            // do sth
        }
        else
        {
            // print a message
        }
        
        // reset the buffer (free its memory and allocate new memory for the next input)
        offset = 0;
        size = read_size;
        free(buff);
        buff = NULL;
        buff = malloc(size + 1);
        *buff = '\0';
    }
    else
    {
        offset += res;
        buff[offset] = '\0';
        
        if (offset + read_size > size)
        {
            size *= 2;
            buff = realloc(buff, size+1);
        }
    }
}

如果这不可能用 read() 我可以尝试其他类似的东西也许是 fgets() ?

So I am trying to take the users input using read(). The while loop runs until ctrl+d is entered in linux terminal and the input is stopped. I wrote some code and it works, but the problem I am running into is that I don't know how to take exactly whats in the input and how to use it.

In the code below, right after if(buff[offset] == '\n') (runs the given input, and after its finished clears the buffer and moves on to the next input) I don't know how to go into the 3 possible cases of input and also I don't know how to get the number from the second case:

  • "e" - exit the program
  • "p N" - do something, N is a number which I also need to get into a variable (I think that buff[offset-1] should get the number but I am not quite sure.)
  • everything else - print a message

Code:

int fd = 0; // set read() to read from STDIN_FILENO, because it's number is 0
const size_t read_size = 1; // set chunk size
size_t size = read_size; 
size_t offset = 0;
size_t res = 0;

char *buff = malloc(size+1);
*buff = '\0';

while((res = read(fd, buff + offset, read_size)) > 0) // read from stdin and save to buff
{       
    if(buff[offset] == '\n')
    { // THIS PART
        buff[offset] = '\0';
        if(buff == "e")
        {
            // exit the program
            return 0;
        }
        else if(buff == "p")
        {
            // do sth
        }
        else
        {
            // print a message
        }
        
        // reset the buffer (free its memory and allocate new memory for the next input)
        offset = 0;
        size = read_size;
        free(buff);
        buff = NULL;
        buff = malloc(size + 1);
        *buff = '\0';
    }
    else
    {
        offset += res;
        buff[offset] = '\0';
        
        if (offset + read_size > size)
        {
            size *= 2;
            buff = realloc(buff, size+1);
        }
    }
}

If this isn't possible with read() I can try with something else like fgets() perhaps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2025-01-17 14:35:59
if(buff == "p")

此行检查字符串 buff 是否与字符串 "p" 存储在同一地址。但是,您不想检查字符串是否存储在同一地址,而是检查它们是否具有相同的内容。

...strcmp() 如何工作...

使用 strcmp() 您可以检查两个字符串是否相同:

if(!strcmp(buff, "e"))

“p N”...因为数字 N 可以是任何数字?

只需检查 buff 的第一个字符是否为 'p'

if(buff[0] == 'p')

请注意,使用单引号 (') 而不是双引号 (") 用于字符常量(与字符串常量相反)。

if(buff == "p")

This line checks if the string buff is stored at the same address as the string "p". However, you don't want to check if the strings are stored at the same address, but if they have the same content.

... how would strcmp() work ...

Using strcmp() you can check if two strings are identical:

if(!strcmp(buff, "e"))

"p N" ... since the number N could be any number?

Simply check if the first character of buff is 'p':

if(buff[0] == 'p')

Note that single quotes (') are used instead of double quotes (") for character constants (in contrast to string constants).

黎歌 2025-01-17 14:35:59

正如我在上面的评论中所述,这是完整的答案:

int main(void)
{
    char line[100], cmd;
    int num, cnt;

    while(fgets(line,sizeof(line),stdin)) != EOF) {       
        cnt = sscanf(line," %c %d",&cmd,&num);
        if (cnt == 0) {
            // empty line, no command
        } else if (cmd == 'e') {
            // exit the program
            break;
        } else if (cmd == 'p'  &&  cnt == 2) {
            // no something with num
        } else {
            // print a message
        }
    }
    return 0;
}

As I described in my comment above, here is a full answer:

int main(void)
{
    char line[100], cmd;
    int num, cnt;

    while(fgets(line,sizeof(line),stdin)) != EOF) {       
        cnt = sscanf(line," %c %d",&cmd,&num);
        if (cnt == 0) {
            // empty line, no command
        } else if (cmd == 'e') {
            // exit the program
            break;
        } else if (cmd == 'p'  &&  cnt == 2) {
            // no something with num
        } else {
            // print a message
        }
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文