在哪里可以找到输入线上的OS系统限制?

时间:2013-12-02 06:38:25

标签: c

这更像是字符缓冲区的设计问题。通常情况下,许多程序,尤其是根据C发现第二版的C程序处理很多字符串的输入和输出。在第5章,第5.3节,第255页,对于那本书上有脚注的人说:

“缓冲区的大小是有争议的。许多程序员将使用80个单元格的数组,因为很少有用户会输入更多。其他人会对输入行的系统限制。”

在哪里可以找到输入线的系统限制?我觉得80个字符的缓冲区太小了。我需要能够解释为什么我在程序项目中选择缓冲区的大小,我的教授会想知道一个原因。

#include <stdio.h>
#include <stdlib.h>
#define DEL 127

int main(int argc, char *argv[]){

    FILE *fpin = NULL;  //pointer to access open file.
    int iochar = 0; //temporary storage for characters taken from stdin or from a file.
    int edit = 1; //used to indicate an edit corresponding to (1)(2)(3)(4)
    char *inbuffer = malloc(80); //temporary storage to store characters taken from file or stdin
    int offset = 0;//places the character taken from file or stdin in correct location in allocated memory

    if (argc == 1){

        while ((iochar = getchar()) != EOF){
            if(offset > 80){
                printf("Error: Input size to big for this program.\n");
                printf("A line from your file is bigger than 80 characters.\n");
                return 0;
            }

            if((iochar<32 && iochar != 10) || iochar == 127){
                edit = 0;
                if(iochar == DEL){
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = '?';
                }
                else{
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = (iochar+64);
                }
            }
            else if(iochar > 127 && iochar < 80)
                edit = 0;
            else if(iochar > 31 && iochar < 127)
                *(inbuffer + offset++) = iochar;

            if(iochar == '\n'){
                *(inbuffer + offset++) = '$';
                int limit_char;
                if(offset > 72)
                    limit_char = offset-72; //if line is larger than 72 print last 72 characters in inbuffer
                else
                    limit_char = 0; //if line is smaller than 72, print whatever is in allocated memory inbuffer

                /*Printing out the characters in allocated memory*/
                *(inbuffer + offset) = 0;
                printf("%s",(inbuffer+limit_char));
                putchar('\n');
                offset = 0;
                }
            }
        return edit;
        }

    if (argc == 2){
        fpin = fopen(argv[1], "r");

        while ((iochar = getc(fpin)) != EOF){
            if(offset > 80){
                printf("Error: Input size to big for this program.\n");
                printf("A line from your file is bigger than 80 characters.\n");
                return 0;
            }

            if((iochar<32 && iochar != 10) || iochar == 127){
                edit = 0;
                if(iochar == DEL){
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = '?';
                }
                else{
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = (iochar+64);
                }
            }
            else if(iochar > DEL && iochar < 80)
                edit = 0;
            else if(iochar > 31 && iochar < DEL)
                *(inbuffer + offset++) = iochar;

            if(iochar == '\n'){
                *(inbuffer + offset++) = '$';
                int limit_char; //used to limit only 72 characters to be printed
                if(offset > 72)
                    limit_char = offset-72; //if line is larger than 72 print last 72 characters
                else
                    limit_char = 0; //if line is smaller than 72

                *(inbuffer + offset) = 0;
                printf("%s",(inbuffer+limit_char));
                putchar('\n');
                offset = 0;
                }
            }
        return edit;
    }
}

1 个答案:

答案 0 :(得分:2)

<limits.h>,它定义了(在类UNIX平台上):

  • MAX_CANON - 终端规范输入行中的最大字节数。
  • MAX_INPUT - 终端输入队列中可用空间的最小字节数;因此,便携式应用程序在读取之前可能需要输入的最大字节数为输入。

  • LINE_MAX - 除非另有说明,否则当实用程序被描述为处理文本文件时,实用程序的输入行(标准输入或其他文件)的最大长度(以字节为单位)。长度包括尾随的空间。

fgets建议在我的Fedora 17盒子和Cygwin上使用LINE_MAX 2048。

另见: