• C String Question

    From Tharius@VERT/ACESHIGH to DOVE.Programming on Tuesday, September 19, 2006 18:59:00
    I'm doing a little side project and I've bumped up on a subborn problem that maybe someone can shed a little insight to.

    I have a structure that contains a nested structure, something like the following :

    struct astruct {
    char *astring;
    int anint;
    astruct *linker;
    };

    struct mystruct {
    char *bstring;
    int bint;
    astruct;
    } invoke;

    If this doesn't compile it's no suprise but I think it illustrates what I'm
    up to. Now... what I want to do is write the structure to a disk file to
    be able to modify it periodically.

    I already have bitten down that I'll have to write a specific output routine so that I can traverse the linked list etc. I know I'll have to use some
    sort of signifier to distinguish wether the next data item I read will be a mystruct or a astruct. Here's the bugger - I can output the strings easily. When I want to reinput and rebuild this structure on later executions, how should I go about it? I can't find advice that fread, gets or scanf will allocate memory if I try to read a string into a char*, so is there a strategy? I can allocate a buffer and try to scan for the null to
    distinguish where the end of the string and then rewind the file but it
    seems a little inefficient.

    Thanks ahead of time

    ---
    þ Synchronet þ [aceshigh.dyn.dhs.org] - Come fly our friendly skies!
  • From Angus McLeod@VERT/ANJO to Tharius on Tuesday, September 19, 2006 23:01:00
    Re: C String Question
    By: Tharius to DOVE.Programming on Tue Sep 19 2006 18:59:00

    Here's the bugger - I can output the strings easily. When I want to
    reinput and rebuild this structure on later executions, how should I go about it?

    Look at fscanf() or consider writing a fixed-length record for each
    struct.

    ---
    Playing: "I Love Ma Guitare" by "Rinocerose"
    from the "Installation Sonore" album
    þ Synchronet þ Programatically generated on The ANJO BBS
  • From Tharius@VERT/ACESHIGH to Angus McLeod on Sunday, December 10, 2006 22:04:00
    "Angus McLeod" <angus.mcleod@VERT/ANJO> wrote in message news:4510AF1E.2178.dove-prg@anjo.com...
    Here's the bugger - I can output the strings easily. When I want to
    reinput and rebuild this structure on later executions, how should I go about it?

    Look at fscanf() or consider writing a fixed-length record for each
    struct.

    I have since had a few lectures on writing dynamic structures to disk. A fixed length record is just too undesirable in this situation because the record size is above a normal disk cluster size. The strategy I've learned
    is to put a flag byte in that will trigger the end of a flag and to just
    suck up the processing time. This works relatively fast if you can put your byte flag on a word boundary, so eg on a 32 bit machine with a structure of say :

    8 characters (8 bytes)
    3 integers (12 bytes)
    {ok to put byte if this is it, same word is guarenteed as long as you're not allocating memory from an arena, eg you use new or malloc etc)

    The boner is you read it byte by byte from a buffer to prevent excessive
    disk access.

    If you're in allocation from arena I don' t know how this might work well.

    ---
    þ Synchronet þ [aceshigh.dyn.dhs.org] - Come fly our friendly skies!