Serializing/deserializing problem
Hi, I'm using a trie for fast storing/retrieval of strings and associated value, and the trie API includes functions for de/serializing the trie. So I need to implement a save() and load() methods, that will call serialize() and deserialize() in trie.c. I'm having problems to get this working, it compiles fine, and I can serialize data to disk, but I can't deserialize data to create a trie from the file. I don't know wheter the problem is in how I write trie's data to
disk, or when I load it from disk, or both.
I copy below the code of a test program that uses the trie API, and I add links to the trie header and definitions. The program reads a file with strings (one per line) and stores them in the trie, storing a value with each string. The serialize function in trie.c will call _write_to_handle() for each key in the trie, and _write_value_to_handle() for each value associated with a key.
Code:
/* Usage:
#test -save /tmp/words.txt read strings, store in trie, and save trie to disk.
#test -load load data from disk and builds a trie from it.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "trie.h"
#define LINESZ 1024
static int total_words = 0;
int total_values=0;
const void *value;
Trie trie;
/* Methods called when serialing trie data */
static int _write_handle(const void *towrite, const int length, void *handle)
{
fwrite(&towrite, length, 1, (FILE *) handle);
return 1;
}
static int _write_value_handle(const void *towrite, void *handle)
{
fwrite(&towrite, sizeof(int), 1, handle);
return 1;
}
/* Methods called when deserialing trie data */
static int _read_handle(void *wasread, const int length, void *handle)
{
printf("_read_handle called. length: %d\n",length);
fread(wasread, length, 1, (FILE *) handle);
printf("wasread is: %s\n",wasread);
return 1;
}
static void * _read_value_handle(void *handle) //returns a pointer to the value
{
//In my tests, this method never gets called, because a problem happens with
// the data read in _read_handle(). No error given, deserialize() function just quits.
int *value;
printf("_read_value_handle called.");
fread(&value, sizeof(int), 1, handle);
printf("value read is: %d",value);
return value;
}
/* Methods to serialize and deserialize the trie */
int trie_save(){
FILE *out = fopen("savefile.txt", "wb");
int success=Trie_serialize(trie, _write_handle, _write_value_handle, (void *)out);
fclose(out);
return success;
}
int trie_load(){
int success=1;
FILE *in = fopen("savefile.txt", "rb");
trie = Trie_deserialize(_read_handle, _read_value_handle,in);
if(!trie) {
success=0;
printf("Error: trie could not be created from data.\n");
}
fclose(in);
return success;
}
int main(int argc, char **argv) {
char s[LINESZ];
int myval;
if(!strcmp(argv[1], "-load")) {
int success=trie_load();
printf("load sucess is: %d\n",success);
}
if(!strcmp(argv[1], "-save")) {
FILE *in = fopen(argv[2], "r"); // e.g. /tmp/words.txt
trie=Trie_new();
value=(const void *)1;
if (!in) {
perror(argv[1]);
return 1;
}
while (fgets(s, LINESZ, in)) {
int len = strlen(s);
if (len) {
s[len-1] = '\0'; // chop newline
total_words++;
// add to trie //arg3 is the value
Trie_set(trie, (const unsigned char*) s, (const void*) total_words);
}
}
fclose(in);
int success=trie_save();
printf("serialized trie success: %d\n",success);
}
fprintf(stderr, "Checked %d total words.\n", total_words);
fprintf(stderr, "Trie length is: %d .\n", Trie_len(trie));
Trie_del(trie);
return 0;
}
trie.h, trie.c
I'd much appreciate any suggestions about what I'm doing wrong.
Thanks.