Click here to Skip to main content
15,885,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int all_digits(char *s){
  for (; *s!=0; s++){
    if (!isdigit(*s)){
      return 0;
    }
  }
  return 1;
}

int main(){
  FILE * fr = fopen("file.csv", "r");

  char save[500], line[200],  to_find[50];
  int oneByOne = 0, numberOfFields=8;
  char *word = NULL;
  printf("Enter the ID card number: ");
  scanf("%s", to_find);

  if(all_digits(to_find) && strlen(to_find) ==  13){
    while(fgets(line, 200, fr)){
      word = strtok(line, "\n");
      strcpy(save, line);

      if (strstr(save, to_find)){
        char *wordone = strtok(save, ",");
        while (wordone != NULL){
          printf("Here are your details: %s\n", wordone);
          wordone = strtok(NULL, ",");
        }
      }
    }
    fclose(fr);

    printf("Found. Hello World!\n");
  }  
  else {
    printf("enter correclty\n");
  }
return 0;
}


What I have tried:

This code has one problem. The problem is in
if(all_digits(to_find) && strlen(to_find) ==  13)
Whenever I try to enter 13 characters but not from the file then it should give me a message of else statement but it is printing Hello World!. Although Hello World! is in if-statement. How to make this work correctly?
Posted
Updated 28-Aug-20 2:18am
v2

1 solution

Quote:
if(all_digits(to_find) && strlen(to_find) == 13)
The above check just validates the input format.

Now you have to check whether the entered 'ID card number' is in the file.

I suggest you to write a function for searching inside the file (just move your search code from main to a function and return the outcome). Then, based on the function return value, your code could show the proper message to the user.
 
Share this answer
 
Comments
Garth J Lancaster 28-Aug-20 8:22am    
+5 :)
CPallini 28-Aug-20 8:54am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900