Well I answered my own question. I put a read and write function inside
the class. The write function writes out the length of each string and
then writes out the actual strings. Then the read function reads in the
lengths and passes these lengths to fread to read in each string. I am
including the two routines below in case there are any gotchas I should be
aware of.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "card.h"

void card::write( FILE *fp )
{
        int sizes[5];
        sizes[0] = strlen(name);
        sizes[1] = strlen(manufacturer);
        sizes[2] = strlen(subset);
        sizes[3] = strlen(number);
        sizes[4] = strlen(condition);

        fwrite( sizes, sizeof(int), 5, fp );
        fwrite( name, sizeof(char), sizes[0], fp );
        fwrite( manufacturer, sizeof(char), sizes[1], fp );
        fwrite( subset, sizeof(char), sizes[2], fp );
        fwrite( number, sizeof(char), sizes[3], fp );
        fwrite( condition, sizeof(char), sizes[4], fp );
        fwrite( &value, sizeof(double), 1, fp);
        fwrite( &year, sizeof(int), 1, fp);
}
void card::read( FILE *fp )
{
        int sizes[5];
        char *ntmp, *mtmp, *stmp, *nutmp, *ctmp;

        fread( sizes, sizeof(int), 5, fp );

        ntmp = (char *)malloc(sizes[0]+1*sizeof(char));
        mtmp = (char *)malloc(sizes[1]+1*sizeof(char));
        stmp = (char *)malloc(sizes[2]+1*sizeof(char));
        nutmp = (char *)malloc(sizes[3]+1*sizeof(char));
        ctmp = (char *)malloc(sizes[4]+1*sizeof(char));

        fread( ntmp, sizeof(char), sizes[0], fp );
                name = strdup( ntmp );
                free(ntmp);
        fread( mtmp, sizeof(char), sizes[1], fp );
                manufacturer = strdup( mtmp );
                free(mtmp);
        fread( stmp, sizeof(char), sizes[2], fp );
                subset = strdup( stmp );
                free(stmp);
        fread( nutmp, sizeof(char), sizes[3], fp );
                number = strdup( nutmp );
                free(nutmp);
        fread( ctmp, sizeof(char), sizes[4], fp );
                condition = strdup( ctmp );
                free(ctmp);
        fread( &value, sizeof(double), 1, fp);
        fread( &year, sizeof(int), 1, fp);
}

Joseph

On Mon, 6 Jul 1998, Joseph Martin wrote:

> Hello,
>       I have a class set up and operating well with several strings, and
> a few doubles or ints. I can set and retrieve these values without any
> trouble by using member functions. Now I am trying to write the class out
> to disk using fwrite and read it in using fread. It writes and reads with
> no errors. However, while the numbers are read back in successfully the
> char strings are not. I think that when the class is being initialized
> with default values the size of the class is set constant. Now when I set
> the variables to custom values could that change the sizeof the class?
> Thus making fread read in the wrong number of bytes? (Am I even lucid
> here?) Should I use a struct instead? Write out each char string
> separately? What is the best method?
> 
> Thanks for the attention.
> 
> Joseph

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
| Joseph Martin:        |      [EMAIL PROTECTED]           |
| Amateur C Programmer, |      http://users.exis.net/~jam/ |
| Web Designer,         |                                  |
| All around tech nut   |                                  |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Reply via email to