c/c++语言开发共享如何使用fgets读取未知长度的输入

我怎么应该使用fgets()读取长输入,我不太明白。

我写了这个

 #include  #include  #include  int main() { char buffer[10]; char *input; while (fgets(buffer,10,stdin)){ input = malloc(strlen(buffer)*sizeof(char)); strcpy(input,buffer); } printf("%s [%d]",input, (int)strlen(input)); free(input); return 0; } 

     #include  #include  #include  int main(void) { char buffer[10]; char *input = 0; size_t cur_len = 0; while (fgets(buffer, sizeof(buffer), stdin) != 0) { size_t buf_len = strlen(buffer); char *extra = realloc(input, buf_len + cur_len + 1); if (extra == 0) break; input = extra; strcpy(input + cur_len, buffer); cur_len += buf_len; } printf("%s [%d]", input, (int)strlen(input)); free(input); return 0; } 

    这是关于最小的更改集,它将为您提供完整的输入。 这样可以一次增加最多9个字节的空间; 这不是最好的方法,但有额外的簿记涉及更好的方式(分配的空间加倍,并记录分配的数量与使用的数量)。 请注意, cur_len记录input指向的空间中的字符串长度,不包括终端null。 另请注意,使用extra可避免因分配失败而导致内存泄漏。

    strcpy()操作可以合法地替换为memmove(input + cur_len, buffer, buf_len + 1) (在这种情况下,你可以使用memcpy()而不是memmove() ,但它在memmove()并不总是有效memmove()总是有效,所以使用memmove()更可靠。


    通过长度加倍 – cur_max变量记录分配了多少空间, cur_len记录了正在使用的空间。

     #include  #include  #include  int main(void) { char buffer[10]; char *input = 0; size_t cur_len = 0; size_t cur_max = 0; while (fgets(buffer, sizeof(buffer), stdin) != 0) { size_t buf_len = strlen(buffer); if (cur_len + buf_len + 1 > cur_max) { size_t new_len = cur_max * 2 + 1; if (buf_len + 1 > new_len) new_len = buf_len + 1; char *extra = realloc(input, new_len); if (extra == 0) break; input = extra; cur_max = new_len; } strcpy(input + cur_len, buffer); cur_len += buf_len; } printf("%s [%d]", input, (int)strlen(input)); free(input); return 0; } 

    更好的方法是使用为您分配的输入机制,例如getline (甚至scanf )。 ( 注意: scanf不会在所有编译器中分配。它在gcc/Linux ,但不在Windows/Codeblocks/gcc

     #include  #include  #include  int main() { char *input; scanf ("%m[^n]%*c", &input); printf("n %s [%d]nn",input, (int)strlen(input)); free(input); return 0; } 

    输出:

     $ ./bin/scanfinput This is my longer string. This is my longer string. [25] 

    getline示例

    需要了解更多c/c++开发分享如何使用fgets读取未知长度的输入,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

     #include  #include  int main() { char *input = NULL; /* input buffer, NULL forces getline to allocate */ size_t n = 0; /* maximum characters to read (0 - no limit */ ssize_t nchr = 0; /* number of characters actually read */ if ((nchr = getline (&input, &n, stdin)) != -1) input[--nchr] = 0; /* strip newline */ printf ("n %s [%zd]nn", input, nchr); free(input); return 0; } 

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

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

      ctvol管理联系方式QQ:251552304

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

      (0)
      上一篇 2021年12月12日
      下一篇 2021年12月12日

      精彩推荐