Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with creating a way to save and load a shoppinglist. As of right now, the program crashes if I try to save

I need help with creating a way to save and load a shoppinglist. As of right now, the program crashes if I try to save an existing shoppinglist or try to load it! How can I modify the code to work according to:

For the saveList() function: When the user chooses to save a shopping list, the user should first select a file name. If you use binary files: Note that the ShoppingList type contains a pointer! This means that you cannot save the whole ShoppingList struct with one fwrite(). You have to save the length field separately first, and then save the dynamic array of items with another fwrite().

For the loadList() function: The user should first be asked for a file name. If the file does not exist, the program should print an error message. Keep in mind that the user may already be working with a shopping list, and that this list should not be destroyed if the file could not be opened. When loading a new shopping list, it should overwrite the previous list that the user was working with. New memory should be allocated for the loaded list. In order to allocate the correct amount of memory, you will have to first load the size of the list and then allocate memory for the list. You can handle this in two ways: either call free() on the old list and call malloc() with the appropriate new size, or you can use realloc(). It is important that you dont get a memory leak, meaning that the old list stays somewhere in memory without being removed. What is important to note here is that you need to let the itemList member point to the new memory that is returned from malloc() or realloc(), if you dont, you may get strange errors such as the list stopping to work once you added a couple of items. When a list has been loaded, it should be possible to work with the loaded file (print, edit, add, delete items) as usual. Make sure to test that it works that way.

My code:

void saveList(struct ShoppingList* list) { FILE* file; //create a pointer to file,which is the variable that will handle the file. char filename[100];

while (getchar() != ' '); printf("Enter filename: "); gets(filename);

file = fopen(filename, "wb"); //We now open the file and "w" makes it possible to write to the file

if (list->itemList < 1) { printf("Error: The list is empty! "); } else if (file != NULL) { fwrite(list->length, sizeof(struct GroceryItem), 1, file); fwrite(list->itemList, sizeof(struct ShoppingList), list->length, file); fclose(file); } return 0; }

void loadList(struct ShoppingList* list) { char fileName[100]; // Open the file for reading while (getchar() != ' '); printf("Enter filename: "); gets(fileName);

FILE* file = fopen(fileName, "rb");

if (fileName == NULL) { printf("Error: Unable to open file %s ", fileName); return; } else if (file != NULL) { fread(fileName, sizeof(char), 100, file); // Read each line of the file char line[256]; while (fgets(line, sizeof(line), file) != NULL) { // Split the line into separate words char* name = strtok(line, " "); char* valueStr = strtok(NULL, " "); int value = atoi(valueStr); // Add the item to the list struct GroceryItem item = { name, value }; addItem(list, item); fclose(file); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Factory Active Database For Enterprise Computing

Authors: Schur, Stephen

1st Edition

0471558443, 9780471558446

More Books

Students also viewed these Databases questions

Question

Draft a proposal for a risk assessment exercise.

Answered: 1 week ago