为什么,如果我进入另一句是scanf函数跳过 [英] Why if I enter sentence other scanf is skipped

查看:183
本文介绍了为什么,如果我进入另一句是scanf函数跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打开输入句子是这样的asdasd ASD asdas伤心任何字符scanf函数将跳过其他scanfs。

有关exapmle如果我键入责任 scanf函数这句话它将为写的义务scanf函数今明scanf函数将被跳过,但全自动将与句字字段 ...

下面是code:

 而(续== 1){
    timeval结构电视;
    焦炭海峡[12];
    结构TM * TM;    INT天= 1;
    焦炭义务[1500];
    烧焦dodatno [1500];    的printf(请输入天,直到义务数:);
    scanf函数(%d个,&安培;天);
    的printf(请输入以下义务:);
    scanf函数(%S,义务);
    的printf(萨提:);
    scanf函数(%S,dodatno);    如果(函数gettimeofday(安培;电视,NULL)== -1)
        返回-1; /* 发生了错误 */
    tv.tv_sec + =天数* 24 * 3600; / *添加6天转换成秒* /
    TM =本地时间(安培; tv.tv_sec);
    / *格式,只要你想* /    的strftime(海峡,sizeof的(STR),D-%B-%%Y,TM);    FILE *数据库;
    数据库= FOPEN(数据库,A +);
    fprintf中(数据库%s |%S |%S \\ n,STR,义务,dodatno);
    FCLOSE(数据库);    看跌期权(为了与添加输入0继续preSS 1 \\ n结束);
    scanf函数(%d个,&安培;续);
    }


解决方案

%S 停止扫描,当它遇到的空白字符(空格,换行等)。使用%[格式说明,而不是:

  scanf函数(%[^ \\ n],义务);
scanf函数(%[^ \\ n],dodatno);

%[^ \\ n] 告诉 scanf函数扫描一切,直到一个换行符。最好是使用长度修改这限制字符数为:

  scanf函数(%1499 [^ \\ n],义务);
scanf函数(%1499 [^ \\ n],dodatno);

在这种情况下,将(末尾的NUL终止子+1)的最大的1499个字符进行扫描。这prevents 缓冲区溢出的。您还可以检查 scanf函数的返回值作为 @EdHeal suggests在注释来检查,如果它是成功的。

If I open enter sentence something like this "asdasd asd asdas sad" for any char scanf it will skip other scanfs.

for exapmle if I type for obligation scanf this sentence it will write for obligation scanf this and next scanf will be skipped but automaticly will be field with sentence word...

Here is the code:

while(cont == 1){
    struct timeval tv;
    char str[12];
    struct tm *tm;

    int days = 1;
    char obligation[1500];
    char dodatno[1500];

    printf("Enter number of days till obligation: ");
    scanf(" %d", &days);
    printf("Enter obligation: ");
    scanf(" %s", obligation);
    printf("Sati: ");
    scanf(" %s", dodatno);

    if (gettimeofday(&tv, NULL) == -1)
        return -1; /* error occurred */
    tv.tv_sec += days * 24 * 3600; /* add 6 days converted to seconds */
    tm = localtime(&tv.tv_sec);
    /* Format as you want */

    strftime(str, sizeof(str), "%d-%b-%Y", tm);

    FILE * database;
    database = fopen("database", "a+");
    fprintf(database, "%s|%s|%s \n",str,obligation,dodatno);
    fclose(database);

    puts("To finish with adding enter 0 to continue press 1 \n");
    scanf(" %d", &cont);
    }

解决方案

%s stops scanning when it encounters a whitespace character (space, newline etc). Use the %[ format specifier instead :

scanf(" %[^\n]", obligation);
scanf(" %[^\n]", dodatno);

%[^\n] tells scanf to scan everything until a newline character. It is better to use the length modifier which limits the number of characters to read:

scanf(" %1499[^\n]", obligation);
scanf(" %1499[^\n]", dodatno);

In this case, it will scan a maximum of 1499 characters (+1 for the NUL-terminator at the end). This prevents buffer overruns. You can also check the return value of scanf as @EdHeal suggests in a comment to check if it was successful.

这篇关于为什么,如果我进入另一句是scanf函数跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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