Search code examples
cstructtext-filesstructurefgets

search string from file and store it in struct using c


i have a structure like so:

struct profile {
    char firstName[15], lastName[15];
    int age, phoneNo;
};

and i've written a code to store the text data from this structure into a text file, like so:

int main()
{
    
    FILE* fPtr;
    fPtr = fopen("profile.txt", "a");

    printf("\n\nPlease enter your details:");
    struct profile c;
    printf("\n\nEnter your first name: ");
    gets(c.firstName);
    printf("\nEnter your last name: ");
    gets(c.lastName);
    printf("\nEnter your age: ");
    scanf("%d", &c.age);
    printf("Enter your phone number: ");
    scanf("%d", &c.phoneNo);

    fprintf(fPtr, "%s#%s#%dy#%d#\n", c.firstName, c.lastName, c.age, c.phoneNo);

    fclose(fPtr);

    return 0;
}

the code above will store the data input into the struct into a text file of strings, each string is one profile, and each value is separated by a '#', like below:

John#Doe#35y#0123456789#
Mary Ann#Brown#20y#034352421#
Nicholas#McDonald#15y#0987654321#

i'd like to know if there's a way i can search for a certain name/age/phoneNo from the text file, select the entire string of the corresponding profile and put each value back into a structure as above so that i can display it? i've separate each value with a '#' so that the program can use the # to differentiate between each value when it reads from the file but i'm not sure how i can separate it when i read the data. should i use fgets ? i'm new to C so i'd appreciate it if someone could explain it me how.


Solution

  • This is not exactly what you are looking for , but it helps you start using fgets and how to search for entries(only strings now).

    #include <stdio.h>
    #include <string.h>
    #define MYFILE "profile.txt"
    #define BUFFER_SIZE 50
    
    int main()
    {
        char nametoSearch[BUFFER_SIZE];
        char Names[BUFFER_SIZE];
        
        FILE* fPtr;
        if (fPtr = fopen(MYFILE, "r"))
        {
            // flag to check whether record found or not
            int fountRecord = 0;
            printf("Enter name to search : ");
            //use fgets if you are reading input with spaces like John Doe
            fgets(nametoSearch, BUFFER_SIZE, stdin);
            //remove the '\n' at the end of string 
            nametoSearch[strlen(nametoSearch)-1] = '\0';
            
            while (fgets(Names, BUFFER_SIZE, fPtr)) 
            {
                // strstr returns start address of substring in case if present
                if(strstr(Names,nametoSearch))
                {
                    printf("%s\n", Names);
                    fountRecord = 1;
                }
            }
            if ( !fountRecord )
                printf("%s cannot be found\n",nametoSearch);
            
            fclose(fPtr);
        }
        else
        {
            printf("file %s  cannot be opened\n", MYFILE );
        }   
        return 0;
    }