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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post almost 3 years ago by Monica Cellio‭.

0 / 255
  • Problems with data structures and filestreams.
  • Removed unnecessary formatting
  • So I just started learning how to use file-streams in C & decided to attempt a question which is to do with library management in C, however I am currently encountering some problems and feel like I need some help/advice.
  • > Basically, my file name is libraryy.txt, I want to delete a book that I have inserted into my libraryy.txt file. When I **enter the book's title into my compiler's output screen, I am expecting the compiler to read from my file (libraryy.txt) & delete that particular book's title, author & subject**. However when I run my program, it doesn't seem to delete anything from my libraryy.txt file.
  • ```
  • /*Data structures that the question wants me to use:*/
  • struct book{
  • char *title;
  • char *author;
  • char *subject;
  • };
  • struct library {
  • struct book collection;
  • int num_books;
  • struct library *next;
  • };
  • struct library* head;
  • struct book collection;
  • struct book *aPtr;
  • struct library aCollection;
  • struct library *aaPtr;
  • ```
  • ```
  • /*Below is my deleteBook function which I'm having a problem with*/
  • void deleteBook(struct library* thislib){
  • //How to define library here?
  • FILE *cfPtr, *cfPtr1;
  • int j, found=0;
  • char stitle[MAX];
  • cfPtr = fopen("libraryy.txt", "r");
  • cfPtr1 = fopen("temp.txt", "w");
  • printf("Enter book title to delete:\n");
  • scanf("%s", stitle);
  • /*I'm confused on what I should be adding beside the '&' operator in the brackets of my 'while' loop*/
  • while(fread(&thislib->collection, sizeof(struct library),1,cfPtr)){
  • if(strcmp(stitle, thislib->collection.title)==0){
  • found = 1;
  • }
  • else
  • fwrite(&thislib->collection, sizeof(library), 1, cfPtr);
  • }
  • fclose(cfPtr);
  • fclose(cfPtr1);
  • if (found){
  • cfPtr1 = fopen("temp.txt", "r");
  • cfPtr = fopen("libraryy.txt", "w");
  • while(fread(&thislib->collection, sizeof(library),1,cfPtr1)){
  • fwrite(&thislib->collection, sizeof(library),1,cfPtr);
  • }
  • fclose(cfPtr);
  • fclose(cfPtr1);
  • }
  • }
  • /*Other irrelevant functions which I did not include here like insertBook().....*/
  • /*Main function*/
  • int main (void){
  • FILE *cfPtr;
  • /*Didn't include the whole coding to avoid the lengthiness of this question*/
  • ```
  • > (Regarding the data structures provided that **we have to use as it is a pre-requisite of the question** as shown and defined in the code, I want to add that it seems that the 'library' data structure is supposed to access the 'book' data structure & the elements in it, so that it will allow me to delete the book (& its author, title as well as subject) from the file (libraryy.txt) when I enter the title of the book in my compiler screen.)
  • > Due to the question providing the data structure to me in that way, I am practically confused on how to access one data structure from another data structure, hence I'm not so sure on what to include beside the **'&' operator** in the while loop brackets as shown below for **fread**... is this how you do it, as shown in my code above? Also how do I define my library in the deleteBook() function?
  • So I just started learning how to use file-streams in C & decided to attempt a question which is to do with library management in C, however I am currently encountering some problems and feel like I need some help/advice.
  • Basically, my file name is libraryy.txt, I want to delete a book that I have inserted into my libraryy.txt file. When I **enter the book's title into my compiler's output screen, I am expecting the compiler to read from my file (libraryy.txt) & delete that particular book's title, author & subject**. However when I run my program, it doesn't seem to delete anything from my libraryy.txt file.
  • ```
  • /*Data structures that the question wants me to use:*/
  • struct book{
  • char *title;
  • char *author;
  • char *subject;
  • };
  • struct library {
  • struct book collection;
  • int num_books;
  • struct library *next;
  • };
  • struct library* head;
  • struct book collection;
  • struct book *aPtr;
  • struct library aCollection;
  • struct library *aaPtr;
  • ```
  • ```
  • /*Below is my deleteBook function which I'm having a problem with*/
  • void deleteBook(struct library* thislib){
  • //How to define library here?
  • FILE *cfPtr, *cfPtr1;
  • int j, found=0;
  • char stitle[MAX];
  • cfPtr = fopen("libraryy.txt", "r");
  • cfPtr1 = fopen("temp.txt", "w");
  • printf("Enter book title to delete:\n");
  • scanf("%s", stitle);
  • /*I'm confused on what I should be adding beside the '&' operator in the brackets of my 'while' loop*/
  • while(fread(&thislib->collection, sizeof(struct library),1,cfPtr)){
  • if(strcmp(stitle, thislib->collection.title)==0){
  • found = 1;
  • }
  • else
  • fwrite(&thislib->collection, sizeof(library), 1, cfPtr);
  • }
  • fclose(cfPtr);
  • fclose(cfPtr1);
  • if (found){
  • cfPtr1 = fopen("temp.txt", "r");
  • cfPtr = fopen("libraryy.txt", "w");
  • while(fread(&thislib->collection, sizeof(library),1,cfPtr1)){
  • fwrite(&thislib->collection, sizeof(library),1,cfPtr);
  • }
  • fclose(cfPtr);
  • fclose(cfPtr1);
  • }
  • }
  • /*Other irrelevant functions which I did not include here like insertBook().....*/
  • /*Main function*/
  • int main (void){
  • FILE *cfPtr;
  • /*Didn't include the whole coding to avoid the lengthiness of this question*/
  • ```
  • Regarding the data structures provided that **we have to use as it is a pre-requisite of the question** as shown and defined in the code, I want to add that it seems that the 'library' data structure is supposed to access the 'book' data structure & the elements in it, so that it will allow me to delete the book (& its author, title as well as subject) from the file (libraryy.txt) when I enter the title of the book in my compiler screen.)
  • Due to the question providing the data structure to me in that way, I am practically confused on how to access one data structure from another data structure, hence I'm not so sure on what to include beside the **'&' operator** in the while loop brackets as shown below for **fread**... is this how you do it, as shown in my code above? Also how do I define my library in the deleteBook() function?

Suggested almost 3 years ago by anatolyg‭