I am making a simple library to store, edit, search books and add new books. I have the following structure:
struct books {
    char bname[150];
    char bauthor[100];
    unsigned bpages,
             price,
             numavail;
} info[MAX];   //array of structures
I have this function to calculate the number of books in the txt file and it works fine:
int numofbooks(FILE *ptr) {
    char ch;
    int num = 0, keep;
    ch = fgetc(ptr);
    while (ch != EOF) {
        if (ch == ':') {
            num++;
        }
        ch = fgetc(ptr);
    }
    return num / 5;
}
and I am using this function to add new books and store then in a txt file and also to the array info. Here the variable p is always 0 even there are books in the file:
void add() {
    int ans = 1;
    static int p;
    char bname[30], bauthor[30];
    unsigned price, bpages, numavail;
    FILE *ptr = fopen("books info.txt", "a");
    system("cls");
    if (ptr == NULL) {
        printf("Couldn't open the file....");
        exit(1);
    }
    p = numofbooks(ptr);  //calling numofbooks to store the number of books in the file in static variable p
    printf("%d", p);  //it always prints 0
    while (ans == 1) {
        printf("\nEnter The name of the book: ");
        fflush(stdin);
        gets(info[p].bname);
        fprintf(ptr, "Book name: %s\n", info[p].bname);
        fflush(stdin);
        printf("Enter The author of this book: ");
        gets(info[p].bauthor);
        fprintf(ptr, "Book author: %s\n", info[p].bauthor);
        fflush(stdin);
        printf("Enter The number of pages of this book: ");
        scanf("%u", &info[p].bpages);
        fprintf(ptr, "Number of pages: %u\n", info[p].bpages);
        fflush(stdin);
        printf("What is the price of the book (in EGP)? ");
        scanf("%u", &info[p].price);
        fprintf(ptr, "Price (in EGP): %u\n", info[p].price);
        printf("Number of books available: ");
        scanf("%u", &info[p].numavail);
        fprintf(ptr, "Number of books available: %u", info[p].numavail);
        fprintf(ptr, "\n---------------------------------------------------------\n");
        printf("\nBook information saved successfully....\n");
        fflush(stdin);
        printf("\nEnter (1) to add another book, (0) to exit or (2) for main menu. ");
        scanf("%d", &ans);
        if (ans == 2) {
            fclose(ptr);
            system("cls");
            main();
        }
    }
}
here is the function I used to search in the file using the name of the author:
void searchauthor(FILE *ptr) {
    char in_name[30];
    int flag = 1;
    ptr = fopen("books info", "r");
    fflush(stdin);
    printf("Enter the name of the author: ");
    gets(in_name);
    int num = numofbooks(ptr);      //num to hold number of books
    for (int i = 0; i < num; i++) {
        if (strcmpi(in_name, info[i].bauthor) == 0) {
            printf("%s %u %u %u", info[i].bname, info[i].price, info[i].numavail, info[i].bpages);
            flag = 0;
        }
    }
    if (flag == 1) {
        printf("Not found.");
    }
}
here is the code that reads data from the file:
        ptr = fopen("books info.txt", "r");
        printf("Total (%d) books are available:-\n\n", numofbooks(ptr));
        ptr = freopen("books info.txt", "r", ptr);
        ch = fgetc(ptr);
        while (ch != EOF) {
            printf("%c", ch);
            ch = fgetc(ptr);
        }
The problem is that this function always prints Not found although the name of the author is already in the txt file. how can I solve this?
 
     
    