简体   繁体   English

C防止输入超过30个字符

[英]C Prevent more than 30 character input

Hi I'm having an issue with my code. 嗨,我的代码有问题。 I need it to run so that if the user enters more than 30 characters for the "fileName" variable it will "return;" 我需要运行它,以便如果用户为“ fileName”变量输入超过30个字符,它将“返回”; but the code I have below doesn't work. 但是我下面的代码不起作用。 Any ideas? 有任何想法吗?

FILE *packetFile; //initialize file handle
char fileName[30]; //initialize
int i;
int length = 0;

length = strlen(fileName);
getchar();

printf("Please enter a file name (Maxium 30 characters): ");
fgets(fileName, 30, stdin);
sscanf(fileName, "%s", fileName);

if(fileName[0] == '\n'){
    return;
}

if(strlen(fileName) > 30)
    return;

if((packetFile = fopen(fileName,"w"))==NULL) {

    printf("Unable to open the file: %s\n",fileName);

} else {

    printf("%d packets have been saved to file named %s", pCount, &fileName);

    for(i=0;i<pCount;i++) {

     fprintf(packetFile,"%04i:%04i:%04i:%04i:%s\r\n",pRecords[i].source,pRecords[i].destination,pRecords[i].type,pRecords[i].port,pRecords[i].data);

    }

        fclose(packetFile);

}

Multiple reasons. 有多种原因。 You're only allocating room for 30 characters, so a strlen will never return a result > 30 in your case. 您只分配30个字符的空间,因此strlen永远不会返回> 30的结果。 You're also only reading max 30 chars in your fgets call. 您在fgets调用中最多只能读取30个字符。

A possible fix is to allocate more space to begin with and use strlen . 可能的解决方法是分配更多空间开始使用strlen Another solution is to keep the limited allocation, and use a loop to read individual characters from stdin - if you don't reach a newline character in time, you just break out and return. 另一个解决方案是保持有限的分配,并使用循环从stdin读取单个字符-如果您没有及时到达换行符,则只需中断并返回即可。

On POSIX systems, getline provides a neat solution. 在POSIX系统上, getline提供了一个简洁的解决方案。 It reads a whole line, and it returns the length. 它读取整行,并返回长度。 Only thing to remember is that getline allocates memory on the heap, so you need to free it when you're done. 唯一要记住的是getline在堆上分配内存,因此您需要在完成后释放它。

Something along the lines of: 类似于以下内容:

char *filename = NULL;
size_t linecap = 0; // These initial values tells getline to allocate as much space as needed
ssize_t linelen = getline(&filename, &linecap, stdin);
if (linelen < 0)
    ...
if (linelen > 30)
    // Treat as error or whatever

free(filename);

(shameless borrowing from the man page) (从手册页无耻地借用)

    char fileName[30+2];//+1 for '\n', +1 for '\0'
    int i;
    int length = 0;

    printf("Please enter a file name (Maxium 30 characters): ");
    fgets(fileName, sizeof(fileName), stdin);

    if(fileName[0] == '\n'){
        return;
    }
    length = strlen(fileName);
    if(fileName[length-1]!='\n'){
        return;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 C 一次输入读取多个字符 - Get C to read more than one character in a single input 如何防止用户输入比C语言要求更多或更少的输入? - How to prevent user from entering more or less input than required in C? 如何通过gcc内联汇编防止错误“ asm中的30个以上操作数” - How to prevent error “more than 30 operands in 'asm'” with inline assesmbly with gcc 倒数算法中奇怪的字符输出,输入超过4个字符 - Weird character output in countdown algorithm with more than 4 char input 扫描字符串时,如果用户输入超过30个字符,如何不允许输入? - When scanning for a string, how do I not allow input if the user inputs more than 30 characters? C printf char连续多于或等于8个字符 - C printf char more than or equal to 8 character in a row manner 在c编程中用多个字符打破while循环 - Breaking a while loop with more than one character in c programming 我怎么知道用户输入的字符是否超过1个? 如果他输入多个字符,我想将其存储在字符数组中 - How can I know if user enters more than 1 character as input? And if he enters more than one character I want to store that in character array C语言输入20位以上的位数 - Input the number of more than 20 digits in C languange C程序数组需要的输入过多 - C program array takes more input than it should
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM