-1

*updated with how i stored the string into my struct

I have a struct as below:

struct patient {
    char name[30], ID[8];
    int age, phoneNo;
};

and i've written the following code:


int searchName()
{
    char search[30];
    char record[60];
    const char s[2] = ",";
    struct patient c;
    char a[8];
    int IDno;

FILE* fPtr;
    fPtr = fopen("patient.txt", "r");

    printf("Enter name to search : ");
    getchar();
    fgets(search, 30, stdin);

    //remove the '\n' at the end of string
    search[strcspn(search, "\n")] = 0;

    while (fgets(record, 60, fPtr))
    {
        // strstr returns start address of substring in case if present
        if (strstr(record, search))
        {
            char* pStr = strtok(record, ",");
            if (pStr != NULL) {
                strcpy(c.ID, pStr);
            }
            pStr = strtok(NULL, ",");
            if (pStr != NULL) {
                strcpy(c.name, pStr);
            }
            pStr = strtok(NULL, ",");
            if (pStr != NULL) {
                c.age = atoi(pStr);
            }
            pStr = strtok(NULL, ",");
            if (pStr != NULL) {
                c.phoneNo = atoi(pStr);
            }
        }

    }

    printf("%s", c.ID);
    strcpy(a, c.ID);
    printf("\n%s", a);
    IDno = atoi(a);
    printf("\n%d", IDno);
    return 0;
}

the code allows me to search for a string in a file, separate the string into smaller strings using strtok, then store them into the struct. suppose i stored a string "PT3" into c.ID in the struct. in my program, I am trying to copy the string of "PT3" from c.ID to a, then convert a to an integer IDno using atoi. I'm not too sure about this, but I think by using atoi to convert "PT3" to an integer, only the integer "3" will remain in IDno, which is exactly when I want.

edit: the problem appears to be that i am unable to convert "PT3" to an integer using atoi. would appreciate help on getting the number "3" from "PT3" to be stored into IDno.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
cloud
  • 105
  • 9
  • post a [mre], the current snippet is wrong for more than one reason. – Sourav Ghosh Oct 23 '20 at 06:55
  • 1
    @SouravGhosh hi, i've updated the code with how i stored the string into my struct. hope it helps. – cloud Oct 23 '20 at 07:03
  • `But currently the string stored in c.ID isn't being copied into a` Why do you think that? Could you post the output of your program? – KamilCuk Oct 23 '20 at 07:04
  • 1
    `but I think by using atoi to convert "PT3" to an integer, only the integer "3" will remain in IDno` it does not work that way. It will continue conversion from beginning till it finds a non-numerical character in the entry. In this case, there's none. – Sourav Ghosh Oct 23 '20 at 07:05
  • @KamilCuk hi, it appears that i overlooked and it's working. sorry for the trouble. now the problem is i can't convert `a` to an integer `IDno`. would appreciate help on that. – cloud Oct 23 '20 at 07:07
  • @SouravGhosh thank you for pointing out the problem in my code. is there any way i could store the "3" from "PT3" into `IDno`? – cloud Oct 23 '20 at 07:11
  • There are many ways, but for that we need to know what the input pattern is. Is it always two chars and then one digit or it can vary? – Sourav Ghosh Oct 23 '20 at 07:12
  • @SouravGhosh sorry but what do you mean by input pattern? how I stored PT3 into the struct? i got the string from a text file of strings, used `strtok` to separate them into specific strings and `strcpy` "PT3" into `c.ID`. the other patient IDs in the file follow the format of PT1, PT2, PT3 ..... PT10 and so on. – cloud Oct 23 '20 at 07:16
  • 1
    So, instead of `atoi()`, you can use `sscanf(a, "PT%d", &IDno);` and do the error check. – Sourav Ghosh Oct 23 '20 at 07:18
  • Don;t edit the question to add the answer. Post an answer which worked for you, and you can even accept your own answer,. – Sourav Ghosh Oct 23 '20 at 07:23
  • 1
    try `atoi( a + 2 );` to skip the first chars – AndersK Oct 23 '20 at 07:27
  • @AndersK Better not to recommend solution involving `atoi()` altogether. Alternative `strtol()` is better. – Sourav Ghosh Oct 23 '20 at 07:53

1 Answers1

1

As per your problem description, looks like you need sscanf(). Something like

sscanf(a, "PT%d", &IDno);

should do the job. Don't forget to do the error check.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261