use*_*960 3 c int input readline
我想让readline接受一个int.完成此任务的最佳方法是什么?我没有接受字符串输入这样的问题:
 char *usrname; // define user input
 /* accept input */
 printf("Enter new name:");
 usrname = readline(NULL);
我明白在接受输入之前有一个int需要对它进行一些错误检查.
Eduardo Costa的回答有效,但它泄漏了记忆.最好定义一个函数来为您处理这个问题:
int readint(char *p, char **e)
{
    char *c = readline(p);
    int i = strtol(c, e, 0);
    if(e)
      {
        size_t o = (size_t)(*e - c),
               l = strlen(*e) + 1;
        *e = malloc(l);
        // error checking omitted
        memcpy(*e, c + o, l);
      }
    free(c);
    return i;
}
该版本甚至可以保留生产线上的任何额外内容,以便您以后可以在需要时使用它.当然,如果你需要用额外的东西做很多事情,你可能最好只阅读该行并自己解析它而不是像这样的函数.