Thread: Serializing classes

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    I think OP knows that Elysia, but it isn't your decision to make.
    Hey, I gave an alternative. I never said anything about people having to use it.
    Also, serializing using >> and << is not confusing at all. Just write a separator after each value, such as '\n'. You just have to watch out for strings since the default separator is a space.
    You won't have to worry about length. You don't have to worry about endianess.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    Hey, I gave an alternative. I never said anything about people having to use it.
    You gave an alternative that was never entirely dismissed by anyone except by the OP when he showed us his attempt to do it in binary.
    Also, serializing using >> and << is not confusing at all. Just write a separator after each value, such as '\n'. You just have to watch out for strings since the default separator is a space.
    No, you still have to delimit strings with care and/or be unambiguous about what is part of the string.
    You won't have to worry about length. You don't have to worry about endianess.
    You might have to worry about length in a text file approach. Perhaps you should read Salem's link too.

    Also endianness isn't really a problem if the file never leaves the host machine, but assumptions like that are stupid. While it's a drawback, binary is no less portable than text just because of endianness. It's actually quite similar to text, (since text is still bits anyway) because of different encodings, if you let that enter the picture.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Just write a separator after each value, such as '\n'.
    I believe '\n' is a legal character inside C++ strings. Hmmm.....I prophesize this thread going around in circles.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by MK27 View Post
    I believe '\n' is a legal character inside C++ strings. Hmmm.....I prophesize this thread going around in circles.
    You can use an escape sequence for those.

    There are many choices for Serializing.

    Does the OP want fast, slim, readable, and/or standard?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by King Mir View Post
    You can use an escape sequence for those.
    Yes, there are 3 simple steps :P

    1) Before you serialize, iterate through all the strings and replace '\n' with an escape sequence, such as "\\n".

    2) After you de-serialize, iterate through all the strings and replace your escape sequence with a newline.

    3) Most importantly, put a big sticker on your class "PLEASE DO NOT USE THE ESCAPE SEQUENCE, \\n, IN ANY STRINGS. THANK YOU, THE MANAGEMENT".

    This idea just keeps getting better and better.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Registered User
    Join Date
    May 2011
    Posts
    29
    I really feel like what im doing is way over-complicated for my purposes, but to the best of my knowledge, read and write deal only with strings, so i cant just tell it to write an integer, also, when i read, it writes the binary value to the specified char array. correct? so i have to use my binti(const char*) (bin to integer function) to get the number back. and for strings, i cant just write the string to the file and then read it right back, it gets converted to binary doesnt it?... im so confused...

  7. #22
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Right. That is clearly an insurmountable problem. The world will now reboot in three...

    Oh, wait, abort the reboot! You can just escape '\' as well exactly as is done for C and C++ string literals. Instances of the literal newline '\n' becomes "\n" (as '\\' followed by 'n') and literal '\\' becomes "\\" (as '\\' followed by '\\') in the target serialization.

    Granted, you still can't "just use operator >> and <<" as those operators will behave weirdly with embedded spacing characters but it isn't as if that is a problem worth more than two seconds of thought since we've already just decided on using literal '\' as an escape character.

    By the way, your (MK27) steps are broken by definition; if the serialization uses literal '\' as an escape character the act of escaping the strings is itself a part of serialization and not a separate operation to be performed before or after.

    Soma

  8. #23
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Whyrusleeping: Go read the link Salem posted.

    It is an excellent link.

    Soma

  9. #24
    Registered User
    Join Date
    May 2011
    Posts
    29
    So, i get that part, but can i just go write("some string", size) and then later just read(*chararray, size) and have chararray contain "some string"? or do i have to break it down and write it letter by letter?

  10. #25
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by MK27 View Post
    Yes, there are 3 simple steps :P

    1) Before you serialize, iterate through all the strings and replace '\n' with an escape sequence, such as "\\n".

    2) After you de-serialize, iterate through all the strings and replace your escape sequence with a newline.

    3) Most importantly, put a big sticker on your class "PLEASE DO NOT USE THE ESCAPE SEQUENCE, \\n, IN ANY STRINGS. THANK YOU, THE MANAGEMENT".

    This idea just keeps getting better and better.
    No, you escape the \(or any chosen escape character) too. Just like in c++.

    Also, as phantomotap says, it generally makes sense to escape charters as you copy, not as a separate step.
    Last edited by King Mir; 06-14-2011 at 11:07 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #26
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    If you use a fixed size or serialize the size, in bytes, (This being what whiteflags has talked about.) you can do something very like that.

    Now, go read that link.

    Soma

  12. #27
    Registered User
    Join Date
    May 2011
    Posts
    29
    I did read it, quite a few times. I think i should specify my question more, does using the read function to read a string from a binary file produce human readable output or is there some 'binary translation' involved?

  13. #28
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Whyrusleeping View Post
    So, i get that part, but can i just go write("some string", size) and then later just read(*chararray, size) and have chararray contain "some string"? or do i have to break it down and write it letter by letter?
    Yeah, you can use write and read like that. Only it's read(&chararray, size).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by King Mir View Post
    No, you escape the \ too. Just like in c++.
    Think about the parsing getting that out. Here's a string:

    "blah/
    hah-ha"

    So before serialization we iterate thru and turn that into:

    "blah///nhah-ha"

    Easy. But to turn that back into what it was, you can't just iterate and replace like you did before -- you are going to need a lookahead/lookback deal. Not so complicated, but unless there's a reason to do so...this is not going to make the task easier.

    Or maybe that's just a matter of style. I defer, point taken. The OP needs to decide whether human readability is important or not, because the later is quicker and simpler.
    Last edited by MK27; 06-14-2011 at 11:22 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #30
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Whyrusleeping View Post
    I did read it, quite a few times. I think i should specify my question more, does using the read function to read a string from a binary file produce human readable output or is there some 'binary translation' involved?
    There is no encoding involved, but nor is the output formatted to read well.

    If you want readable, then generally you want strings that are escaped or with clear delimiters, as in C++0X raw strings.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  3. Serializing/deserializing problem
    By vsla in forum C Programming
    Replies: 3
    Last Post: 04-21-2008, 03:55 PM
  4. Serializing problem.. (can't use >> operator)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2005, 11:10 AM
  5. Serializing a class
    By Prog.Patterson in forum C++ Programming
    Replies: 4
    Last Post: 10-27-2005, 10:21 PM