gpt4 book ai didi

c - fgets 被跳过/不停止输入

转载 作者:行者123 更新时间:2023-11-30 16:36:31 25 4
gpt4 key购买 nike

我正在尝试在简单的 while 循环中使用 fgets。 fgets 只是没有被调用。

完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_BUFF 20
#define OSPID //Here is where I would put the ospid I was interested in

int readContentsOfProcessMapsFile(int target);


int main(int argc, char** argv) {

char lineprompt[LINE_BUFF];
char command1[LINE_BUFF], command2[LINE_BUFF];
int flag;


if( readContentsOfProcessMapsFile(OSPID) != 0)
{
printf("Error reading file");
exit(EXIT_FAILURE);
}

while(1) {

fflush(stdout);
printf("enter name:>");
if ( fgets(lineprompt, sizeof(lineprompt), stdin) == NULL)
{
printf("Error using fgets\n");
break;
}

sscanf(lineprompt, "%s %s", &command1, &command2);

if (strcmp(command1, "show") == 0)
{
if( (strcmp(command2, "active") == 0) )
{
flag = 1;
}
else if( (strcmp(command2, "inactive") == 0) )
{
flag = 2;
}
else
{
flag =0;
}
printf("run show function on ospid file");
}
else if (strcmp(command1, "exit") == 0)
{
printf("run exit/cleanup");
break;
}
else if (strcmp(command1, "help") == 0)
{
printf("run help function");
continue;
}
else {
printf("Nothing entered, run help function");
continue;
}
}
}

int readContentsOfProcessMapsFile(int target) {
FILE *fd; // /proc/<target>/maps
char name[128], *line = NULL, *match = NULL;
size_t len = 0;
char* filename = NULL;

snprintf(name, sizeof (name), "/proc/%u/maps", target);

if ((fd = fopen(name, "r")) == NULL) {
printf("error");
return (EXIT_FAILURE);
}

while (getline(&line, &len, fd) != -1)
{
unsigned long start, end;
char read, write, exec, cow;
int offset, dev_major, dev_minor, inode;

filename = realloca(filename, len);

sscanf(line, "%p-%p %c%c%c%c %x %x:%x %u %[^\n]", &start, &end, &read,
&write, &exec, &cow, &offset, &dev_major, &dev_minor, &inode, filename);
match = strstr(filename, "/ora_");
if (match != NULL)
{
printf("##lowAddr:%p, highAddr:%p and filename:%s\n", start, end, name);
}
}
free(filename);
free(line);
if (fclose(fd) == -1))
{
printf("Failed to close %s", fd);
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}

我尝试在 while 循环之后添加 fseek(stdin,0,SEEK_END);fflush(stdin);,但是 fgets() 仍然被跳过。我知道无论如何使用 fflush(stdin) 都不是一个好习惯。

发生这种情况时,getchar() 会打印一个正方形或上面有两个点的 Y。那么我是否正确地认为 fgets() 被跳过的原因是它拾取了这些字符?谁能提供一种方法来阻止/防止这种情况?

感谢大家的评论。我已将其范围缩小到上面的 readContentsOfProcessMapsFile 函数。如果我将其注释掉,那么 fgets 将按预期工作。

现在我修正了有关 close(fd) 的拼写错误,它似乎并没有真正关闭文件!刚刚发现我自己的错误!更改为 fclose 但看到同样的问题

最佳答案

我正在尝试在简单的 while 循环中使用 fgets。 fgets 只是没有被调用。...

当你试图解决的问题被干扰所包围时,你很可能将所有的精力都花在了干扰上,而不是问题上。如图所示,您的代码不仅包含您描述的问题,还包含一堆干扰因素。

这些评论为clean up your distractions提供了一些建议。 。以下是我在不受干扰的情况下的看法:

一个非常简单的循环读取 stdin 的工作 fgets 示例:

#include <stdio.h>

int main(void)
{
char temp[BUFSIZ]; //BUFSIZ defined as 512 in stdio on my system.

while((fgets(temp,BUFSIZ,stdin)) != NULL)
printf("%s\n",temp);

return 0;
}

EOF 将导致循环退出。 (在 Windows 上按 ctrl-C 停止)

当你让它工作时,慢慢开始在它周围包含其他所需的功能,每次都验证添加的功能不会破坏代码。随着您的进步,您可能会偶尔遇到错误,当发生这种情况时,请使用 step-wise, pragmatic approach to determining the problem ,然后继续。

关于c - fgets 被跳过/不停止输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48420442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com