Persist changes in C
Posted
by Mohit Deshpande
on Stack Overflow
See other posts from Stack Overflow
or by Mohit Deshpande
Published on 2010-04-14T20:27:37Z
Indexed on
2010/04/14
21:53 UTC
Read the original article
Hit count: 392
I am developing a database-like application that stores a a structure containing:
struct Dictionary
{
char *key;
char *value;
struct Dictionary *next;
};
As you can see, I am using a linked list to store information. But the problem begins when the user exits out of the program. I want the information to be stored somewhere. So I was thinking of storing the linked list in a permanent or temporary file using fopen, then, when the user starts the program, retrieve the linked list. Here is the method that prints the linked list to the console:
void PrintList()
{
int count = 0;
struct Dictionary *current;
current = head;
if (current == NULL)
{
printf("\nThe list is empty!");
return;
}
printf(" Key \t Value\n");
printf(" ======== \t ========\n");
while (current != NULL)
{
count++;
printf("%d. %s \t %s\n", count, current->key, current->value);
current = current->next;
}
}
So I am thinking of modifying this method to print the information through fprintf instead of printf and then the program would just get the infomation from the file. Could someone help me on how I can read and write to this file? What kind of file should it be, temporary or regular? How should I format the file (like I was thinking of just having the key first, then the value, then a newline character)?
© Stack Overflow or respective owner