Thread: Serializing/deserializing problem

  1. #1
    Registered User
    Join Date
    Oct 2007

    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.

    /* 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: &#37;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); 
      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) {
          printf("Error: trie could not be created from data.\n");
      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.

    Last edited by vsla; 04-21-2008 at 02:59 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    Maybe you should do some error checking with your fread() and fwrite(). You always basically tell trie that its results were fine and dandy, but perhaps they aren't. And what sort of value is _read_handle() and _write_handle() supposed to return? fread() and fwrite() return the amount of data processed upon success, not 1.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > _write_handle, _write_value_handle
    Why are you writing to the file in BOTH of these functions?

    > fwrite(&towrite, length, 1, (FILE *) handle);
    Don't you mean
    fwrite( towrite, length, 1, (FILE *) handle);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Thanks for the answers.

    >Maybe you should do some error checking with your fread() and fwrite()
    I had checked this and I saw it could both read/write some data from/to the file.

    >Why are you writing to the file in BOTH of these functions?
    because first function writes the key in the trie, and the second function handles the value associated to that key.

    >Don't you mean fwrite( towrite, length, 1, (FILE *) handle);
    That's what I had initially, but it gave me a segmentation fault, and I changed it as '&towrite' and the seg fault disappeared. Now I have tried to change that only for arg1 of the two functions that write/read the value (not the key), and everything works fine!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM