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 »
Q&A

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.

Post History

40%
+2 −4
Q&A Problems with data structures and filestreams.

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 l...

2 answers  ·  posted 2y ago by dumplings‭  ·  edited 2y ago by Alexei‭

#7: Post edited by user avatar Alexei‭ · 2022-06-26T16:24:17Z (almost 2 years ago)
rollback to a revision that actually contains meaningful information
  • Can someone briefly explain how struct actually works in data structures?
  • 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?
#6: Post edited by user avatar dumplings‭ · 2022-06-26T14:08:16Z (almost 2 years ago)
  • 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?
  • Can someone briefly explain how struct actually works in data structures?
#5: Post edited by user avatar Monica Cellio‭ · 2021-12-20T14:12:13Z (over 2 years ago)
fixed title damaged in otherwise-good edit
  • Removed unnecessary formatting
  • Problems with data structures and filestreams.
#4: Post edited by user avatar anatolyg‭ · 2021-12-20T14:11:52Z (over 2 years ago)
  • 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?
#3: Post edited by user avatar dumplings‭ · 2021-12-13T15:32:30Z (over 2 years ago)
  • 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(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?
#2: Post edited by user avatar dumplings‭ · 2021-12-13T15:32:02Z (over 2 years ago)
  • 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 */
  • 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)){ //I'm confused on what I should put beside the '&' operator
  • 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(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?
#1: Initial revision by user avatar dumplings‭ · 2021-12-13T15:29:55Z (over 2 years ago)
Problems with data structures and filestreams.
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 */
    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)){ //I'm confused on what I should put beside the '&' operator 
            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?