Search code examples
ciofile-handling

why is my search function for file handling(.txt file) not working?


I want to search the structure (and print other details of book) I have added for the book for a particular issueNumber entered by user in the .txt file.

When I run the code it terminates after asking for user input (issueNumber-issuenumber).

I don't understand the issue behind this, I think there there is some issue with the while loop (code inside it).

Also I am not finding any good resource on file handling on the internet (maybe only for text mode). It would be helpful if someone suggest good resource.

Below is my code for searching for issueNumber and printing other details too.

My structure:

struct data
{
    char author[25];
    int issueNumber;
    char title[25];
    int issued;
};

My search function:

void searchBookByIssueNumber(FILE *fp)
{
    struct data data2;
    data2.issued = 0;
    int issuenumber = 0;
    printf("enter issue no.");

    scanf("%d", &issuenumber);

    while (fscanf(fp, "%s %d %s %d\n", data2.author, &data2.issueNumber, data2.title, &data2.issued) != EOF)
    {

        if (data2.issueNumber == issuenumber)
        {
            fseek(fp, -5, SEEK_CUR);
            printf("%s %d %s", data2.author, data2.issueNumber, data2.title);
        }
    }
    fclose(fp);
}

Start function:

void start(FILE *fp)
{
    printf("---------------------------------------------------\n");
    printf("|                                                 |\n");
    printf("|      WELCOME TO LIBRARY MANAGEMENT              |\n");
    printf("|                                                 |\n");
    printf("---------------------------------------------------\n");

    /*login();*/
    /*addBook(fp);*/
    searchBookByIssueNumber(fp);
}

Main function:

int main()
{
    FILE *fp;
    fp = fopen("c1.txt", "w+");
    start(fp);

    return 0;
}

Solution

  • Here is a working version of your code (I didn't fix gets but it's definitely something you shoud do, more info in 3. and I removed all the unused functions for the sample)

    The corrected issues are:

    1.

    while (fscanf(fp, "%s %d %s %d\n", data2.author, 
    &data2.issueNumber, data2.title, &data2.issued) != EOF) {/*...*/}
    
    • When you read ints you need to pass to scanf the address of the variable where you want to store the value. data2.issueNumber needs to be &data2.issueNumber, data2.issued needs to be &data2.issued.

    • I used != EOF condition and it produced an infinite loop in my compiler(check it here), though this may not always happen, using == 4 condition is, IMO, a better way to do it.

    • "%s is not a safe specifier, it's vulnerable to buffer overflow, use "%24s" for a 25 char container.

    In all it should be:

    while (fscanf(fp, "%24s %d %24s %d\n", data2.author, 
    &data2.issueNumber, data2.title, &data2.issued) == 4) {/*...*/}
    

    2. The file is being whiped when you open it, I used "r" or "r+" flag.


    3. (This one is not corrected, the link has info on how to replace it)

    gets() is a deprecated function, though some compilers still have it, this is an extremely vulnerable function and you should not use it.