0

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;
}

1 Answers1

0

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • @user:6865932 , I edited the code to correct those things before you answered, but the result is still the same. – KARAN GUPTA Jun 23 '20 at 14:21
  • Can it be bacause of the way you open the file, `w+` will clear the file. – anastaciu Jun 23 '20 at 14:24
  • @user:6865932, gets is not operational when I use search function – KARAN GUPTA Jun 23 '20 at 14:26
  • @KARANGUPTA I added a corrected version of the code to the answer https://wandbox.org/permlink/UEdNqPVX3kJ6iDVm, input is 5566 for the sample. – anastaciu Jun 23 '20 at 15:05
  • @ anastaciu, what changes have you made apart from changing mode to r+, I see !=EOF also works., what were the error in my code? – KARAN GUPTA Jun 23 '20 at 15:08
  • @KARANGUPTA, the changes I made are explained in the answer, `!= EOF` should produce an infinite loop, check it here https://wandbox.org/permlink/3WN5iQW0pQBqGLld – anastaciu Jun 23 '20 at 15:12
  • @KARANGUPTA, I changed the flag to `"r+"`, I changed the `scanf` with the corrections I mentioned, and I removed `strcmp` and used `==`. That's basically it. – anastaciu Jun 23 '20 at 15:14
  • @KARANGUPTA, I see you corrected the `strcmp` in the question, it's something that shouldn't be done otherwise the answers and comments will look to be out of context. – anastaciu Jun 23 '20 at 15:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216505/discussion-between-karan-gupta-and-anastaciu). – KARAN GUPTA Jun 23 '20 at 15:21