【问题标题】:How to use fscanf to read from file and find the matched record with the user input如何使用 fscanf 从文件中读取并通过用户输入找到匹配的记录
【发布时间】:2017-07-05 08:20:29
【问题描述】:

我想读取用户输入并验证它是否与“user.txt”记录匹配,如果匹配则会提示用户错误消息,但scanf读取的数据总是会变成随机的号码,这是代码。对不起,我的英语不是以英语为母语的人

                int day,month,year;
                char name[15];


                printf("Enter your name\n");
                scanf(" %[^\n]s",name);
                printf("Enter you birthday(dd/mm/yyyy)\n");
                scanf("%d%*[-/]%d%*[-/]%d",&day,&month,&year);
                printf("Enter your contact number (60-)\n");
                scanf("%d",&contactNumber);
                printf("Enter your postcode\n");
                scanf("%d",&postcode);

                char x [30];
                int y,z,w,d;
                int duplicateContactNum [15];
                int checkSentinel=0;

                filepointer=fopen("user.txt","r");
                if(filepointer==NULL){
                //Not exist,Print exception message
                printf("Exception Occur: Error writing text into the text file");

                }
                while(!feof(filepointer))
                {
                    fscanf(filepointer,"%s;%d/%d/%d;%d;%d\n",x,&y,&z,&w,&duplicateContactNum,&d);
                    if(strcmp(contactNumber,duplicateContactNum)==0)
                    {
                        checkSentinel++;
                        break;
                    }

                }
                if(checkSentinel!=0)
                {
                    rewind(filepointer);
                    printf("You have entered a duplicated entry\n");
                    fclose(filepointer);
                }
                else
                {
                    rewind(filepointer);
                    fclose(filepointer);
                    filepointer=fopen("user.txt","a");
                    fprintf(filepointer,"%s;%d/%d/%d;%d;%d\n",name,day,month,year,contactNumber,postcode);
                    fclose(filepointer);
                }

【问题讨论】:

    标签: c file scanf strcmp


    【解决方案1】:

    您在初始格式字符串中有一个错误,并且您没有测试 scanf 调用的返回值,因此所有返回错误但您没有收到警告。

                printf("Enter your name\n");
                if (1 != scanf(" %[^\n]",name)) {  // no s after [] format
                    fprintf(stderr, "Error reading name\n");
                    return 1;
                }
                printf("Enter you birthday(dd/mm/yyyy)\n");
                if (3 != scanf("%d%*[-/]%d%*[-/]%d",&day,&month,&year)) {
                    ...
                }
                printf("Enter your contact number (60-)\n");
                if (1 != scanf("%d",&contactNumber)) {
                    ...
                }
                printf("Enter your postcode\n");
                if (1 != scanf("%d",&postcode)) {
                    ...
                }
    

    记住:始终测试输入函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 2021-03-23
      • 2017-10-22
      • 2019-07-05
      • 1970-01-01
      • 2011-07-10
      • 2021-04-23
      • 1970-01-01
      相关资源
      最近更新 更多