Вопрос

So I have an assignment to build an address book in C where the data is saved every time the program is closed (so I have to use files). Also I'll have to read from the address book file any entries that already exist. The program should also offer the option to sort by name, surname, email etc.

I thought of implementing this using a linked list. The problem is I'm not sure if I should use a linked list or a hash table. Because by using a hash table I don't exactly know how I will be able to sort the data. Is it okay to use a linked list and sort only by changing the pointers to the next entry?

I have one more question though. Let's say for instance, that I get from the user his name using fgets() and later on print it to the file like this: fprintf (Addressbook, "Name: %s", addressbook.name);. When I'm trying to read from the file and load the pre-existing entries into my linked list how am I going to exclude the "Name: " part which is in the file?

For example, if the user inputs the name "Chris" then it will be written to the file like this: Name: Chris. But after I close the program and execute it again, I want to read from the file and load the name "Chris" ONLY in addressbook.name. Overall, I'm pretty confused with reading from the file and generally loading my linked list

Any help would be much appreciated. Thank you for your time!

Это было полезно?

Решение

First question: Linked list or hash table for address book:
Answer: SQL. Really. This sort of conundrum for how to store and retrieve data has an answer and NEVER SHALL THOU ROLL THINE OWN*.

*Except if you're a student and learning how to roll.

Which you are. And this is an assignment. So both options are "wrong", but since it's just a learning exercise, go for it. Whatever man. Yeah, go with a linked list and re-arrange the nodes every time you want to sort it by a different parameter. That sounds like a fun yet horrifying experience.

Second questions: When I'm trying to read from the file and load the pre-existing entries into my linked list how am I going to exclude the "Name: " part which is in the file?

fscanf(f_file, "Name: %s", nameArrayTypeThing);

And hope they don't have spaces in their name and the file isn't corrupted. It'll be good enough for an assignment.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top