Search code examples
cfilescanfstrcmp

How to use fscanf to read from file and find the matched record with the user input


I want to read the user input and verify if it is match with the "user.txt" record, if it match then it will prompt the user an error message,but the data read by the scanf will always turn into random number,here's the code.Sorry for my English as i'm not a native English speaker

                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);
                }

Solution

  • You have one error in the initial format string, and you do not test the return value of the scanf calls, so the all return errors but you are not warned about it.

                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)) {
                    ...
                }
    

    Remember: Always test input functions