1

First, the while loop is not comparing titles in the file and the num_books does not add on top of the file. Second, instead of reading the num_books already in the file, it reads twice every time executed. can someone help?

typedef struct book {
    char *title;
    char *author;
    char *subject;
};

struct library {
    struct book collection;
    int num_books;
    struct library *next;
};

add(FILE *file, FILE *fileout, struct library *thislib, struct book *thisbook)     {
    char choice = "1";
    int size;
    char line1[50];
    file = fopen("temp1.txt", "a+");
    fileout = fopen("temp2.txt", "a+");
    printf("\t Add a book to your library!\n");
    printf("\t\t Title: ");
    scanf("%s", &thisbook->title);
    printf("\t\t Author: ");
    scanf("%s", &thisbook->author);
    printf("\t\t Subject: ");
    scanf("%s", &thisbook->subject);
    fseek(file, 0, SEEK_SET);
    thislib->num_books++;
    fprintf(file, "%d", thislib->num_books);
    size = ftell(file);
    while (thislib->num_books > 1) {
        for (int i = 1; i < size; i++) {
            fseek(file, i, SEEK_CUR);
            fscanf(file, "%s", line1);
            printf("%s", line1);
            if (strcmp(thisbook->title, line1) != 0) {
                printf("Please re-enter title as similar ones are not allowed.\n");
                printf("\t\t Title: ");
                scanf("%s", &thisbook->title);
                strcpy(line1, "");
            }
        }
    }
    fseek(file, 1, SEEK_SET);
    fprintf(file, "%c  %s  %s  %s\n", choice, &thisbook->title, &thisbook->author, &thisbook->subject);
    fprintf(fileout, "The book %s, author %s, subject %s has been added to the library.\n", &thisbook->title, &thisbook->author, &thisbook->subject);
    fclose(file);
    fclose(fileout);
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Was memory allocated for `thisbook->title`, `thisbook->author`, and `thisbook->subject`? – Barmar Dec 18 '21 at 16:39
  • `char choice="1";` is incorrect. You need to enclose a `char` literal in single quotes, not double quotes. – Barmar Dec 18 '21 at 16:41
  • 2
    The `while` loop is an infinite loop, because you never update `thislib->num_books`. – Barmar Dec 18 '21 at 16:48
  • The loop is reading from the file when it's already at the end of the file, so there's nothing to read. – Barmar Dec 18 '21 at 16:51
  • Since you opened the file in append mode, `fprintf(file, "%d", thislib->num_books);` is going to write the number at the end of the file. Is that what you wanted? – Barmar Dec 18 '21 at 16:53
  • I'm struggling to figure out what this function is supposed to be doing. – Barmar Dec 18 '21 at 16:54
  • @Barmar memory was allocated and ive made the changes like you said but the problem is still in comparing – user17591552 Dec 18 '21 at 16:55
  • @Barmar the num_books is supposed to be at the top thats why i used fseek – user17591552 Dec 18 '21 at 16:56
  • this function is to add books and if the titles are same, then the line in file is erased – user17591552 Dec 18 '21 at 16:57
  • But you opened the file in `a+` mode. `a` means that all writing happens at the end of the file. Seeking to the beginning doesn't change that. – Barmar Dec 18 '21 at 16:57
  • I don't understand what you're trying to do with `fseek(file,i,SEEK_CUR);`. Do you think that goes to the next line of the file? `fseek()` doesn't know anything about lines, that goes forward by 1 byte, not a whole line. – Barmar Dec 18 '21 at 17:01
  • see [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/q/284236/995714) for why "urgent" won't help you – phuclv Dec 19 '21 at 10:57
  • @phuclv well it's urgent because it's due and if someone actually helped I wouldn't have to edit and put the urgent there – user17591552 Dec 19 '21 at 11:19
  • 2
    All the questions are equally urgent here. And writing something like that in the title is most likely not going to get you help faster -- most likely the exact opposite happens. – James Z Dec 19 '21 at 11:28
  • If your task is due, you spent too little time before, in paying attention in class, asking while there was time, coding while there was time, and so on. ;-) And I know, I went through all of this, but at an age without internet. – the busybee Dec 19 '21 at 13:19
  • @thebusybee you've been a great help, good to know you know my situation so well – user17591552 Dec 19 '21 at 13:52

0 Answers0