Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

Why doesn't my deletefile() function work? [closed]

+0
−4

Closed as unclear by Alexei‭ on Dec 12, 2021 at 16:44

This question cannot be answered in its current form, because critical information is missing.

This question was closed; new answers can no longer be added. Users with the reopen privilege may vote to reopen this question if it has been improved or closed incorrectly.

Question: Why doesn't my deletefile() function work. I have already also created a temporary file called TempFile. I want to delete specific contents in my "libraryy.txt" file (yes, it is called libraryy.txt NOT library.txt file XD) anyways, did I make a mistake somewhere in my deletefile() function?

-Also there are already contents in my libraryy.txt file which I inserted. What my deletefile() function should do is basically, when I opt for '2' in my switch case statement, it will ask me to enter the book title and then it will delete the record of the book with that particular title. But its not working TT

#include<stdlib.h>
#include<conio.h>
#define MAX 49

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

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


struct book collection; //supposed to be aBook
struct book *aPtr;
struct library aCollection;
struct library *aaPtr;

int count = 0;
int size;

void deletefile(){
    FILE *fptr1, *fptr2;
    char stitle[MAX];

    printf("Enter title to delete: ");
    scanf("%s", &stitle);

    if(stitle == '\0'){
        printf("ERROR, No book record found!");
    }
    else{
        fptr1 = fopen("libraryy.txt", "r");
        fptr2 = fopen("TempFile.txt", "w");
        while(fread(&collection, sizeof(collection), 1, fptr2)){
            if (collection.title != stitle){
                fwrite(&collection, sizeof(collection), 1, fptr1);
            }
            fclose(fptr1);
            fclose(fptr2);
            fptr1 = fopen("libraryy.txt", "w");
            fptr2 = fopen("TempFile.txt", "r");
            printf("The book %s has been removed from the library\n", stitle);
            fclose(fptr1);
            fclose(fptr2);
        }

    }

}



void operate(){
     printf("-----------------Operations--------------\n");
     printf("1) Add a book to a collection\n");
     printf("2) Delete a book from the collection by a particular title\n");
     printf("3) Delete all books from the collection by a particular author\n");
     printf("4) Search for a book from the collection by a particular title\n");
     printf("5) Search for all books from the collection by a particular author\n");
     printf("6) List all of the books by a particular author from the collection\n");
     printf("7) List all of the books by a particular subject from the collection\n");
     printf("8) List all of the books in the original order\n");
}


int main (void){
   FILE *cfPtr, *cfPtr2; 

   char strtitle[MAX];
   char strauthor[MAX];
   char strsubject[MAX];
   int operation, test;
   char curr;
   int count=0;


    if ((cfPtr = fopen("libraryy.txt","a")) == NULL){
        printf("File could not be opened...\n");
    }

   else {

        operate();
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

Details about the "my deletefile() function work?" part (1 comment)
Again, read your code... (2 comments)
Why C for this program? (1 comment)

0 answers