Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Serialize a variable length structure

Serialize a variable length structure

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hatemtaleb
    wrote on last edited by
    #1

    Hello, I want to serialize structure like this: struct FRAME{ int Len;// size of Text following char Text[]; } ; the problem is that this is not a fixed length variable! how could I read the structures after being serialized? Thanks

    _ S 2 Replies Last reply
    0
    • H hatemtaleb

      Hello, I want to serialize structure like this: struct FRAME{ int Len;// size of Text following char Text[]; } ; the problem is that this is not a fixed length variable! how could I read the structures after being serialized? Thanks

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Read 4 bytes first to get the length value. Now read the number of bytes identified by length.

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • H hatemtaleb

        Hello, I want to serialize structure like this: struct FRAME{ int Len;// size of Text following char Text[]; } ; the problem is that this is not a fixed length variable! how could I read the structures after being serialized? Thanks

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        The way it's usually done is something like this:

        • Read Len from whatever you're reading from
        • Allocate a buffer of size Len + sizeof Len bytes.
        • Copy Len into the first sizeof Len bytes of the buffer
        • Read Len bytes into the buffer after the copied value of Len
        • Cast the buffer pointer to FRAME* and return it

        Let's presume you're reading from a C FILE:

        FRAME* ReadFRAME(FILE* file)
        {
        int Len;
        fread((void*)&Len, sizeof(Len), 1, file);
        char* space = (char*)malloc(Len + sizeof(Len));
        FRAME* gotFrame = (FRAME*)space;
        gotFrame->Len = Len;
        fread((void*)gotFrame->Text, 1, Len, file);
        return gotFrame;
        }

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups