C标准输入限制阻止我读取超过1200个字符的行

C标准输入限制阻止我读取超过1200个字符的行,c,stdin,fgets,C,Stdin,Fgets,我有一个简单的程序: #include <stdio.h> #include <stdlib.h> #include <string.h> size_t strlen(const char *str) { const char *s; for (s = str; *s; ++s); return(s - str); } /* * Initial size of the read buffer */ #define DEFAULT

我有一个简单的程序:

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

size_t strlen(const char *str)
{
    const char *s;
    for (s = str; *s; ++s);
    return(s - str);
}

/*
 * Initial size of the read buffer
 */
#define DEFAULT_BUFFER 1024

/*
 * Standard boolean type definition
 */
typedef enum { false = 0, true = 1 } bool;

/*
 * Flags errors in pointer returning functions
 */
bool has_err = false;

/*
 * Reads the next line of text from file and returns it.
 * The line must be free()d afterwards.
 *
 * This function will segfault on binary data.
 */
char *readline(FILE *file){
    char *buffer   = NULL;
    char *tmp_buf  = NULL;
    bool line_read = false;
    int  iteration = 0;
    int  offset    = 0;

    if(file == NULL){
        fprintf(stderr, "readLine: NULL file pointer passed!\n");
        has_err = true;

        return NULL;
    }

    while(!line_read){
        if((tmp_buf = malloc(DEFAULT_BUFFER)) == NULL){
            fprintf(stderr, "readLine: Unable to allocate temporary buffer!\n");
            if(buffer != NULL)
                free(buffer);
            has_err = true;

            return NULL;
        }

        if(fgets(tmp_buf, DEFAULT_BUFFER, file) == NULL){
            free(tmp_buf);

            break;
        }

        if(tmp_buf[strlen(tmp_buf) - 1] == '\n') /* we have an end of line */
            line_read = true;

        offset = DEFAULT_BUFFER * (iteration + 1);

        if((buffer = realloc(buffer, offset)) == NULL){
            fprintf(stderr, "readLine: Unable to reallocate buffer!\n");
            free(tmp_buf);
            has_err = true;

            return NULL;
        }

        offset = DEFAULT_BUFFER * iteration - iteration;

        if(memcpy(buffer + offset, tmp_buf, DEFAULT_BUFFER) == NULL){
            fprintf(stderr, "readLine: Cannot copy to buffer\n");
            free(tmp_buf);
            if(buffer != NULL)
                free(buffer);
            has_err = true;

            return NULL;
        }

        free(tmp_buf);
        iteration++;
    }

    return buffer;
}

int main (int argc, char *argv[])
{
    int rows = 0, cols = 0;
    char *line = readline(stdin);

    printf("%s", line);

    return 0;
}
#包括
#包括
#包括
大小字符串(常量字符*str)
{
常量字符*s;
对于(s=str;*s;++s);
返回(s-str);
}
/*
*读取缓冲区的初始大小
*/
#定义默认缓冲区1024
/*
*标准布尔类型定义
*/
typedef枚举{false=0,true=1}bool;
/*
*指针返回函数中的标志错误
*/
bool has_err=false;
/*
*从文件中读取下一行文本并将其返回。
*之后,该行必须是空闲的。
*
*此函数将对二进制数据进行故障隔离。
*/
char*readline(文件*FILE){
char*buffer=NULL;
char*tmp_buf=NULL;
bool line_read=false;
int迭代=0;
整数偏移=0;
if(file==NULL){
fprintf(stderr,“读取线:传递了空文件指针!\n”);
has_err=true;
返回NULL;
}
而(!line_read){
if((tmp_buf=malloc(默认缓冲区))==NULL){
fprintf(stderr,“readLine:无法分配临时缓冲区!\n”);
if(缓冲区!=NULL)
自由(缓冲);
has_err=true;
返回NULL;
}
if(fgets(tmp_buf,默认_缓冲区,文件)==NULL){
免费(tmp_buf);
打破
}
如果(tmp_buf[strlen(tmp_buf)-1]=='\n')/*我们有一个结束行*/
行_read=true;
偏移量=默认缓冲区*(迭代+1);
如果((缓冲区=realloc(缓冲区,偏移量))==NULL){
fprintf(stderr,“readLine:无法重新分配缓冲区!\n”);
免费(tmp_buf);
has_err=true;
返回NULL;
}
偏移量=默认缓冲区*迭代-迭代;
if(memcpy(缓冲区+偏移量,tmp_buf,默认缓冲区)==NULL){
fprintf(stderr,“readLine:无法复制到缓冲区\n”);
免费(tmp_buf);
if(缓冲区!=NULL)
自由(缓冲);
has_err=true;
返回NULL;
}
免费(tmp_buf);
迭代++;
}
返回缓冲区;
}
int main(int argc,char*argv[])
{
int行=0,cols=0;
字符*行=读取行(标准输入);
printf(“%s”,第行);
返回0;
}
但是,当我尝试在控制台中输入超过1200个字符时,程序停止在这行代码中工作:
fgets(tmp\u buf,默认缓冲区,文件)

问题是:如何在不施加任何内部限制的情况下使C从
stdin
读取


p.S.Niether如果我尝试使用控制台管道操作符将我需要输入的内容管道化到程序中,程序就会工作(即,
我不知道您在代码中做了什么,但这就是您的问题标题

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

char *readline(FILE *file)
 {
    char  *line;
    size_t length;
    size_t count;
    int    chr;

    length = 100;
    line   = malloc(1 + length);
    if (line == NULL)
     {
        fprintf(stderr, "memory exhausted!\n");
        return NULL;
     }
    count = 0;
    while (((chr = fgetc(file)) != EOF) && (chr != '\n'))
     {
        if (count >= length)
         {
            void *pointer;
            length += length;
            pointer = realloc(line, 1 + length);
            if (pointer == NULL)
             {
                fprintf(stderr, "memory exhausted!\n");
                free(line);
                return NULL;
             }
            line = pointer;
         }
        line[count] = chr;
        count      += 1;
     }
    line[count] = '\0';

    return line;
 }

int main(void)
 {
    char *line = readline(stdin);
    if (line != NULL)
        printf("%s\n", line);
    free(line);
    return 0;
 }
#包括
#包括
char*readline(文件*FILE)
{
字符*行;
尺寸与长度;
大小/数量;
int-chr;
长度=100;
直线=malloc(1+长度);
如果(行==NULL)
{
fprintf(stderr,“内存耗尽!\n”);
返回NULL;
}
计数=0;
而(((chr=fgetc(file))!=EOF)和&(chr!='\n'))
{
如果(计数>=长度)
{
void*指针;
长度+=长度;
指针=realloc(线,1+长度);
if(指针==NULL)
{
fprintf(stderr,“内存耗尽!\n”);
自由线;
返回NULL;
}
直线=指针;
}
行[计数]=chr;
计数+=1;
}
行[计数]='\0';
回流线;
}
内部主(空)
{
字符*行=读取行(标准输入);
如果(行!=NULL)
printf(“%s\n”,第行);
自由线;
返回0;
}

此代码中唯一的限制是内存,即使内存不足,代码也不会失败,因为正如您所看到的那样,这已得到处理。

我不知道您在代码中做了什么,但这就是您的问题标题

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

char *readline(FILE *file)
 {
    char  *line;
    size_t length;
    size_t count;
    int    chr;

    length = 100;
    line   = malloc(1 + length);
    if (line == NULL)
     {
        fprintf(stderr, "memory exhausted!\n");
        return NULL;
     }
    count = 0;
    while (((chr = fgetc(file)) != EOF) && (chr != '\n'))
     {
        if (count >= length)
         {
            void *pointer;
            length += length;
            pointer = realloc(line, 1 + length);
            if (pointer == NULL)
             {
                fprintf(stderr, "memory exhausted!\n");
                free(line);
                return NULL;
             }
            line = pointer;
         }
        line[count] = chr;
        count      += 1;
     }
    line[count] = '\0';

    return line;
 }

int main(void)
 {
    char *line = readline(stdin);
    if (line != NULL)
        printf("%s\n", line);
    free(line);
    return 0;
 }
#包括
#包括
char*readline(文件*FILE)
{
字符*行;
尺寸与长度;
大小/数量;
int-chr;
长度=100;
直线=malloc(1+长度);
如果(行==NULL)
{
fprintf(stderr,“内存耗尽!\n”);
返回NULL;
}
计数=0;
而(((chr=fgetc(file))!=EOF)和&(chr!='\n'))
{
if(计数>=长度)
{
void*指针;
长度+=长度;
指针=realloc(线,1+长度);
if(指针==NULL)
{
fprintf(stderr,“内存耗尽!\n”);
自由线;
返回NULL;
}
直线=指针;
}
行[计数]=chr;
计数+=1;
}
行[计数]='\0';
回流线;
}
内部主(空)
{
字符*行=读取行(标准输入);
如果(行!=NULL)
printf(“%s\n”,第行);
自由线;
返回0;
}

此代码中唯一的限制是内存,即使内存不足,代码也不会失败,因为正如您所看到的那样,内存已经足够了。

问题不在于您的程序,而在于终端驱动程序。终端驱动程序将保留的未读字符数有一个上限。它希望行比han有一些限制,可能是1024或1200或其他值。如果您尝试键入更多字符,它们将不会被接受

您可以尝试按Control-D(在Unix上;在Windows上按Control-Z)从终端驱动程序发送挂起的行,然后继续使用更多字符。这应该可以工作,但必须一直计数到1199(或任何比限制小一的数字)会造成麻烦然后点击Control-D。如果数据是从某个地方复制粘贴的,那么这将是一个双重的麻烦,因此您不能选择在适当的时间间隔点击Control-D

您也可以尝试使用。这需要小心-您需要恢复