在c中使用read()获取输入
所以我尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行检查字符串
buff
是否与字符串"p"
存储在同一地址。但是,您不想检查字符串是否存储在同一地址,而是检查它们是否具有相同的内容。使用
strcmp()
您可以检查两个字符串是否相同:只需检查
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.Using
strcmp()
you can check if two strings are identical:Simply check if the first character of
buff
is'p'
:Note that single quotes (
'
) are used instead of double quotes ("
) for character constants (in contrast to string constants).正如我在上面的评论中所述,这是完整的答案:
As I described in my comment above, here is a full answer: