与fgets被限制为1024字节 - 我可以用呢? [英] fgets is limited to 1024 bytes - what can I use instead?

查看:499
本文介绍了与fgets被限制为1024字节 - 我可以用呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从大小16000bytes的用户输入的字符串来读取。 与fgets 只读取1024个字节。我可以用呢?我写在C,这是我的code现在。难道是我没有使用malloc?

 字符海峡[16392]而(fprintf中(标准错误,>中!),与fgets(STR,16392,标准输入)的feof(标准输入)){}

此外,readline的似乎工作。

 而(行=的readline(>中!)的feof(标准输入)){
    的printf(您输入:%S \\ n,行);
    免费(线);
  }


解决方案

不要更换与fgets()有更坏的选择。 与fgets()没有限制。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;    字符*缓冲区;
    为size_t的大小;
    大小=千万; / * *10米/
    缓冲=的malloc(大小);
    (!缓存),如果/ * *误差/;
    如果(与fgets(缓冲区大小,标准输入)== NULL)/ *异常* /;
    // ...
    免费(缓冲);

I want to read from user's input string of size 16000bytes. fgets only reads 1024 bytes. What can I use instead? I am writing in c and this is my code right now. Is it that I am not using malloc?

char str[16392];

while(fprintf(stderr, "> "), fgets(str, 16392, stdin), !feof(stdin)) { }

Also, readline seems to work.

  while(line = readline("> "), !feof(stdin)) {
    printf("You entered: %s\n", line);
    free(line);  
  }

解决方案

Do not replace fgets() with a worse option. fgets() has no limit.

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

    char *buffer;
    size_t size;
    size = 10000000; /* 10m */
    buffer = malloc(size);
    if (!buffer) /* error */;
    if (fgets(buffer, size, stdin) == NULL) /* error */;
    // ...
    free(buffer);

这篇关于与fgets被限制为1024字节 - 我可以用呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆