如何读取用户在C中输入的字符串? [英] How do I read a string entered by the user in C?

查看:190
本文介绍了如何读取用户在C中输入的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取用户使用C程序输入的名称。



为此,我写道:

 字符名称[20]; 

printf(输入名称:);
gets(name);

但是使用 gets 不好,所以有什么更好的方法?

解决方案

您应该从不使用 gets (或 scanf ,具有无限制的字符串大小),因为这样可以使您避免缓冲区溢出。将 fgets stdin 句柄一起使用,因为它允许您限制将要放置在缓冲区中的数据。 / p>

以下是我用于用户输入的代码段:

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

#定义OK 0
#定义NO_INPUT 1
#定义TOO_LONG 2
static int getLine(char * prmpt,char * buff,size_t sz){
int ch,额外;

//获得带有缓冲区溢出保护的行。
if(prmpt!= NULL){
printf(%s,prmpt);
fflush(stdout);
}
if(fgets(buff,sz,stdin)== NULL)
返回NO_INPUT;

//如果时间太长,将没有换行符。在这种情况下,我们将
//刷新到行尾,以免超出部分不会影响下一个调用。
if(buff [strlen(buff)-1]!=‘\n’){
extra = 0;
while(((((ch = getchar())!='\n')&&(ch!= EOF)))
extra = 1;
return(extra == 1)? TOO_LONG:好的;
}

//否则删除换行符并将字符串返回给调用方。
buff [strlen(buff)-1] =‘\0’;
返回确定;
}

这允许我设置最大大小,将检测是否有太多数据在该行中输入,并且也会刷新该行的其余部分,因此不会影响下一个输入操作。



您可以使用以下命令进行测试:

  //测试getLine()的程序。 

int main(void){
int rc;
个char buff [10];

rc = getLine( Enter string>,buff,sizeof(buff));
if(rc == NO_INPUT){
//额外的NL,因为我的系统不会在EOF上输出该值。
printf( \n无输入\n);
返回1;
}

if(rc == TOO_LONG){
printf(输入太长[%s] \n,buff);
返回1;
}

printf( OK [%s] \n,buff);

返回0;
}


I want to read the name entered by my user using C programmes.

For this I wrote:

char name[20];

printf("Enter name: ");
gets(name);

But using gets is not good, so what is a better way?

解决方案

You should never use gets (or scanf with an unbounded string size) since that opens you up to buffer overflows. Use the fgets with a stdin handle since it allows you to limit the data that will be placed in your buffer.

Here's a little snippet I use for line input from the user:

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

#define OK       0
#define NO_INPUT 1
#define TOO_LONG 2
static int getLine (char *prmpt, char *buff, size_t sz) {
    int ch, extra;

    // Get line with buffer overrun protection.
    if (prmpt != NULL) {
        printf ("%s", prmpt);
        fflush (stdout);
    }
    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;

    // If it was too long, there'll be no newline. In that case, we flush
    // to end of line so that excess doesn't affect the next call.
    if (buff[strlen(buff)-1] != '\n') {
        extra = 0;
        while (((ch = getchar()) != '\n') && (ch != EOF))
            extra = 1;
        return (extra == 1) ? TOO_LONG : OK;
    }

    // Otherwise remove newline and give string back to caller.
    buff[strlen(buff)-1] = '\0';
    return OK;
}

This allows me to set the maximum size, will detect if too much data is entered on the line, and will flush the rest of the line as well so it doesn't affect the next input operation.

You can test it with something like:

// Test program for getLine().

int main (void) {
    int rc;
    char buff[10];

    rc = getLine ("Enter string> ", buff, sizeof(buff));
    if (rc == NO_INPUT) {
        // Extra NL since my system doesn't output that on EOF.
        printf ("\nNo input\n");
        return 1;
    }

    if (rc == TOO_LONG) {
        printf ("Input too long [%s]\n", buff);
        return 1;
    }

    printf ("OK [%s]\n", buff);

    return 0;
}

这篇关于如何读取用户在C中输入的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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