30/09/2018, 17:03

[C] Cần giúp thêm một hàm vào để xóa sách trong file

Em có một bài yêu cầu làm một chương trình quản lý thư viện nhưng em chỉ làm dược phần thêm sách vào file rồi show ra. Nhưng không biết cách làm thế nào để xóa sách ra khỏi danh sách sách.
code của em đây

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct list_book
{
    char book_id[10];
    char book_name[20];
    char book_author[20];
} book_title;

//************************************************
void addbook(FILE *f)
{
    int n,i;
    book_title book;
    printf("Enter the number book that you want to add: ");
    scanf("%d",&n);
    f = fopen("booktitle.txt","a");

    for(i=1;i<=n;i++)
    {
        fflush(stdin);
        printf("Enter book ID: "); gets(book.book_id);
        printf("Enter book name: ");gets(book.book_name);
        printf("Enter book author: ");gets(book.book_author);
        fwrite(&book,sizeof(struct list_book),1,f);
        fflush(stdin);
    }
    fclose(f);
}
//****************************************************************************
void readfile(FILE *f)
{
    int i=0;

    book_title book;
    f = fopen("booktitle.txt","r");
    printf("                   LIST ALL BOOKS IN THE LIBRARY
");
    printf("|------------------------------------------------------------
");
    printf("|%-s|%-20s|%-30s|
","Book ID","Book name","Book author");
    printf("|-----------------------------------------------------------
");
    fread(&book,sizeof(struct list_book),1,f);
    while (!feof(f))
    {
        printf("|%-7s|%-20s|%-30s|
",book.book_id,book.book_name,book.book_author);
        fread(&book,sizeof(book),1,f);


    }

    fclose(f);
}
//**************************************************************************
void search(FILE *f)
{

    book_title book;
    char id[10];
    int i=0,found=0;

    printf("What ID do you need?
");
    fflush(stdin);
    gets(id);
    f = fopen("booktitle.txt","r+");
    while (!feof(f) && found == 0)
    {
        fread(&book,sizeof(struct list_book),1,f);
        if ( strcmp(book.book_id,id)==0) found=1;
        i++;
    }
    if (found != 1)
        printf("ID is not exist!!");
    else
    {
        printf("Found the book: 
");
        printf("ID: %s
",book.book_id);
        printf("Name: %s
",book.book_name);
        printf("Author: %s

",book.book_author);


    }

    fclose(f);
}






int main()
{
    FILE *f;
    int num;
    printf("choose cases that you want:
");
    printf("1. Add books into libary:
");
    printf("2. List all books:
");
    printf("3. Find book information:
");
    scanf("%d",&num);
    //while(num != 4) {
    switch(num)
    {
case 1:
    addbook(f);
    getch();
    break;
case 2:
    readfile(f);
    getch();
    break;
case 3:
    search(f);
    getch;
    break;
case 4:
    remove(f);
    break;

    }
    //}
    return 0;
}

Sau khi em chạy chương trình em ra được kết quả:

và em muốn thêm vào dòng bạn có muốn xóa thông tin đó.

Shin viết 19:06 ngày 30/09/2018

Theo ngu ý của mình thì khi muốn xóa sách thì bạn cắt thông tin sách cuối cùng ra sau đó chép đè lên chỗ sách bạn muốn cắt là đc
Ví dụ như thư viện là:
A
B
C
D
khi muốn xóa sách B thì bạn lấy D đè lên chỗ B để thành ra
A
D
C
là ok

Thai Hoc Nguyen viết 19:13 ngày 30/09/2018

Bạn cũng có thể làm bài này theo cách làm Mảng thông thường mà nhỉ

Lê Thành Long viết 19:12 ngày 30/09/2018

mình cũng định làm vậy nhưng không biết sao đưa dữ liệu file vào mảng struct dc đây

Bài liên quan
0