c/c++语言开发共享使用fgets从c中的stdin读取未知长度行

我试图使用C语言从stdin读取未知长度行。

我在网上看到了这个:

char** str; gets(&str); 

但它似乎给我带来了一些问题,我真的不明白如何以这种方式做到这一点。

你能解释一下为什么这个例子工作/不工作以及实现它的正确方法是什么(使用malloc?)

    您不希望指向char指针,使用char数组

     char str[128]; 

    或指向char的指针

     char *str; 

    如果选择指针,则需要使用malloc保留空间

     str = malloc(128); 

    然后你可以使用fgets

     fgets(str, 128, stdin); 

    并删除trailling换行符

     char *ptr = strchr(str, 'n'); if (ptr != NULL) *ptr = ''; 

    要读取任意长行,可以使用getline (添加到libc的GNU版本的函数):

     #define _GNU_SOURCE #include  char *foo(FILE * f) { int n = 0, result; char *buf; result = getline(&buf, &n, f); if (result < 0) return NULL; return buf; } 

    或使用fgetsrealloc实现自己的实现:

     char *getline(FILE * f) { size_t size = 0; size_t len = 0; size_t last = 0; char *buf = NULL; do { size += BUFSIZ; /* BUFSIZ is defined as "the optimal read size for this platform" */ buf = realloc(buf, size); /* realloc(NULL,n) is the same as malloc(n) */ /* Actually do the read. Note that fgets puts a terminal '' on the end of the string, so we make sure we overwrite this */ if (buf == NULL) return NULL; fgets(buf + last, size, f); len = strlen(buf); last = len - 1; } while (!feof(f) && buf[last] != 'n'); return buf; } 

    用它来称呼它

     char *str = getline(stdin); if (str == NULL) { perror("getline"); exit(EXIT_FAILURE); } ... free(str); 

    更多信息

    首先, gets()无法防止缓冲区溢出。 这使得它从最新的C标准中删除它是如此危险。 不应该使用它。 但是,通常的用法是这样的

     char buffer[20]; gets(buffer); /* pray that user enters no more than 19 characters in a line */ 

    您的用法是将指向指向char的指针的指针传递gets() 。 这不是gets()期望的,所以你的代码甚至都不会编译。

    评论中反映的祷告元素是为什么gets()如此危险。 如果用户输入20个(或更多)字符, gets()将很乐意在buffer末尾写入数据。 程序员无法在代码中阻止这种情况(缺少访问硬件来电子输入输入过多数据的用户,这超出了标准C的范围)。

    然而,要回答您的问题,唯一的方法是分配一些大小的缓冲区,以某种受控方式读取数据直到达到该大小,如果需要重新分配以获得更大的大小,并继续直到换行(或者结束 – 遇到文件或输入上的一些其他错误情况。

    malloc()可用于初始分配。 malloc()realloc()可用于重新分配(如果需要)。 请记住,当不再需要数据时,必须释放以这种方式分配的缓冲区(使用free() ) – 否则结果是内存泄漏。

    使用getline()函数,这将返回行的长度,以及指向已分配内存区域中行内容的指针。 (确保在完成后将行指针传递给free())

    如果你想输入未知长度的字符串或输入尝试使用以下代码。

     #include  #include  #include  int main() { char *m; clrscr(); printf("please input a stringn"); scanf("%ms",&m); if (m == NULL) fprintf(stderr, "That string was too long!n"); else { printf("this is the string %sn",m); /* ... any other use of m */ free(m); } getch(); return 0; } 

    注意%ms,%和GNU扩展一样..

      以上就是c/c++开发分享使用fgets从c中的stdin读取未知长度行相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/559640.html

      (0)
      上一篇 2021年1月27日
      下一篇 2021年1月27日

      精彩推荐