Writing a structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rogério Brito

    #16
    Re: Writing a structure

    E. Robert Tisdale wrote:[color=blue]
    > Rogério Brito wrote:[color=green]
    >> The second option would be better, because, AFAIK, an exact binary
    >> image may have problems with the size of the members of the
    >> structure, with the alignment chosen by the compiler for the
    >> members of the structure and even byte-endianness, if the output
    >> file is to be used across different platforms.[/color]
    >
    > The "textual version" may have problems with precise representation
    > of floating-point numbers.[/color]

    Indeed. I had not thought about the case of floating point numbers and
    the problems that some libraries might have writing their (differing)
    representation to files.

    But since the "binary dump version" might also have problems with this
    (since one can't guarantee that the systems where the file is written
    and where the file is read use the same size and organization of
    floating point numbers) in addition to those that I pointed above, I'd
    still choose the "textual version" before going for the "binary dump"
    version.

    Of course, nothing beats a properly done, specialized serialization library.

    --
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Rogério Brito - rbrito@ime.usp. br - http://www.ime.usp.br/~rbrito
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    Comment

    • Rogério Brito

      #17
      Re: Writing a structure

      E. Robert Tisdale wrote:[color=blue]
      > Rogério Brito wrote:[color=green]
      >> The second option would be better, because, AFAIK, an exact binary
      >> image may have problems with the size of the members of the
      >> structure, with the alignment chosen by the compiler for the
      >> members of the structure and even byte-endianness, if the output
      >> file is to be used across different platforms.[/color]
      >
      > The "textual version" may have problems with precise representation
      > of floating-point numbers.[/color]

      Indeed. I had not thought about the case of floating point numbers and
      the problems that some libraries might have writing their (differing)
      representation to files.

      But since the "binary dump version" might also have problems with this
      (since one can't guarantee that the systems where the file is written
      and where the file is read use the same size and organization of
      floating point numbers) in addition to those that I pointed above, I'd
      still choose the "textual version" before going for the "binary dump"
      version.

      Of course, nothing beats a properly done, specialized serialization library.

      --
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Rogério Brito - rbrito@ime.usp. br - http://www.ime.usp.br/~rbrito
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      Comment

      • Dan Pop

        #18
        Re: Writing a structure

        In <c4uhl8$2ms802$ 1@ID-218953.news.uni-berlin.de> =?ISO-8859-1?Q?Rog=E9rio_B rito?= <rbrito@ime.usp .br> writes:
        [color=blue]
        >Mike Wahler wrote:[color=green]
        >> "Glen" <glenmendez@yah oo.co.in> wrote in message
        >> news:eb99b94a.0 404052318.582c6 6f8@posting.goo gle.com...[color=darkred]
        >>>is it using fwrite??[/color]
        >>
        >> That's what you'd use to write an exact binary
        >> image, yes. Another way would be to write
        >> a textual version, one member at a time.[/color]
        >
        >The second option would be better, because, AFAIK an exact binary image
        >may have problems with the size of the members of the structure, with
        >the alignment choosen by the compiler for the members of the structure
        >and even byte-endianness, if the output file is to be used across
        >different platforms.[/color]

        OTOH, the output file might be supposed to be used *only* by the same
        program, on the same platform.

        To decide what option is better, one must know the program specification.

        Dan
        --
        Dan Pop
        DESY Zeuthen, RZ group
        Email: Dan.Pop@ifh.de

        Comment

        • Dan Pop

          #19
          Re: Writing a structure

          In <c4uhl8$2ms802$ 1@ID-218953.news.uni-berlin.de> =?ISO-8859-1?Q?Rog=E9rio_B rito?= <rbrito@ime.usp .br> writes:
          [color=blue]
          >Mike Wahler wrote:[color=green]
          >> "Glen" <glenmendez@yah oo.co.in> wrote in message
          >> news:eb99b94a.0 404052318.582c6 6f8@posting.goo gle.com...[color=darkred]
          >>>is it using fwrite??[/color]
          >>
          >> That's what you'd use to write an exact binary
          >> image, yes. Another way would be to write
          >> a textual version, one member at a time.[/color]
          >
          >The second option would be better, because, AFAIK an exact binary image
          >may have problems with the size of the members of the structure, with
          >the alignment choosen by the compiler for the members of the structure
          >and even byte-endianness, if the output file is to be used across
          >different platforms.[/color]

          OTOH, the output file might be supposed to be used *only* by the same
          program, on the same platform.

          To decide what option is better, one must know the program specification.

          Dan
          --
          Dan Pop
          DESY Zeuthen, RZ group
          Email: Dan.Pop@ifh.de

          Comment

          • Keith Thompson

            #20
            Re: Writing a structure

            Dan.Pop@cern.ch (Dan Pop) writes:[color=blue]
            > In <eb99b94a.04040 52318.582c66f8@ posting.google. com>
            > glenmendez@yaho o.co.in (Glen) writes:
            >[color=green]
            > >Is it possible to write a structure to a file in c...as in c++...??
            > >is it using fwrite??[/color]
            >
            > There is more than one way of doing it. The two most popular approaches
            > are:
            >
            > 1. Use a text file, and convert each field of the structure to a textual
            > representation, using fprintf.
            >
            > 2. Use a binary file and dump the binary representation of the
            > structure value with fwrite.
            >
            > The first approach is more portable (the value can be read on a different
            > platform, or even with a program written in a different language), but
            > uses more disk space and CPU cycles.
            >
            > The second approach uses less disk space and CPU cycles, but the resulting
            > binary file can be read only by a C program compiled with the same
            > compiler, on the same platform.[/color]
            [...]

            You're not guaranteed that the binary file can be read by a C program
            compiled with a different compiler or on a different platform, but
            *sometimes* it can be, if you're very careful and/or very lucky.

            For different compilers on the same platform, compiler writers
            typically use the same layout algorithms to allow data portability.
            (This is not guaranteed; it's something you need to verify.)

            It's not an approach I'd recommend, but I've worked on a system that
            shares binary data files between VAX and Alpha, and on another that
            shared binary data files between 68k and SPARC. In the former case,
            we had to be careful to use the same floating-point formats on both
            systems; it's not clear that the system could have met its performance
            constraints if we had used a textual format. In the latter, I had to
            insert dummy character array members to force common alignment; it was
            ugly, but it worked.

            This kind of thing is unlikely to work if the platforms have different
            byte ordering.

            But if you want to share binary data, be aware that you're going to be
            spending a lot of your time keeping everything consistent, and
            probably dealing with problems when things aren't consistent.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • Keith Thompson

              #21
              Re: Writing a structure

              Dan.Pop@cern.ch (Dan Pop) writes:[color=blue]
              > In <eb99b94a.04040 52318.582c66f8@ posting.google. com>
              > glenmendez@yaho o.co.in (Glen) writes:
              >[color=green]
              > >Is it possible to write a structure to a file in c...as in c++...??
              > >is it using fwrite??[/color]
              >
              > There is more than one way of doing it. The two most popular approaches
              > are:
              >
              > 1. Use a text file, and convert each field of the structure to a textual
              > representation, using fprintf.
              >
              > 2. Use a binary file and dump the binary representation of the
              > structure value with fwrite.
              >
              > The first approach is more portable (the value can be read on a different
              > platform, or even with a program written in a different language), but
              > uses more disk space and CPU cycles.
              >
              > The second approach uses less disk space and CPU cycles, but the resulting
              > binary file can be read only by a C program compiled with the same
              > compiler, on the same platform.[/color]
              [...]

              You're not guaranteed that the binary file can be read by a C program
              compiled with a different compiler or on a different platform, but
              *sometimes* it can be, if you're very careful and/or very lucky.

              For different compilers on the same platform, compiler writers
              typically use the same layout algorithms to allow data portability.
              (This is not guaranteed; it's something you need to verify.)

              It's not an approach I'd recommend, but I've worked on a system that
              shares binary data files between VAX and Alpha, and on another that
              shared binary data files between 68k and SPARC. In the former case,
              we had to be careful to use the same floating-point formats on both
              systems; it's not clear that the system could have met its performance
              constraints if we had used a textual format. In the latter, I had to
              insert dummy character array members to force common alignment; it was
              ugly, but it worked.

              This kind of thing is unlikely to work if the platforms have different
              byte ordering.

              But if you want to share binary data, be aware that you're going to be
              spending a lot of your time keeping everything consistent, and
              probably dealing with problems when things aren't consistent.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              Schroedinger does Shakespeare: "To be *and* not to be"

              Comment

              • kal

                #22
                Re: Writing a structure

                > Is it possible to write a structure to a file in c...as in c++...??[color=blue]
                > is it using fwrite??[/color]

                Convert the "data" in the structure to an XML document.

                Comment

                • kal

                  #23
                  Re: Writing a structure

                  > Is it possible to write a structure to a file in c...as in c++...??[color=blue]
                  > is it using fwrite??[/color]

                  Convert the "data" in the structure to an XML document.

                  Comment

                  • Barry Schwarz

                    #24
                    Re: Writing a structure

                    On Tue, 06 Apr 2004 09:15:51 -0700, "E. Robert Tisdale"
                    <E.Robert.Tisda le@jpl.nasa.gov > wrote:
                    [color=blue]
                    >I used Google
                    >
                    > http://www.google.com/
                    >
                    >to search for
                    >
                    > +"C++" +"serialization "
                    >
                    >and I found lots of stuff.[/color]

                    You actually search on C++ to find stuff about C?


                    <<Remove the del for email>>

                    Comment

                    • Barry Schwarz

                      #25
                      Re: Writing a structure

                      On Tue, 06 Apr 2004 09:15:51 -0700, "E. Robert Tisdale"
                      <E.Robert.Tisda le@jpl.nasa.gov > wrote:
                      [color=blue]
                      >I used Google
                      >
                      > http://www.google.com/
                      >
                      >to search for
                      >
                      > +"C++" +"serialization "
                      >
                      >and I found lots of stuff.[/color]

                      You actually search on C++ to find stuff about C?


                      <<Remove the del for email>>

                      Comment

                      • E. Robert Tisdale

                        #26
                        Re: Writing a structure

                        Barry Schwarz wrote:
                        [color=blue]
                        > E. Robert Tisdale wrote:
                        >[color=green]
                        >>I used Google
                        >>
                        >> http://www.google.com/
                        >>
                        >>to search for
                        >>
                        >> +"C++" +"serialization "
                        >>
                        >>and I found lots of stuff.[/color]
                        >
                        > You actually search on C++ to find stuff about C?[/color]

                        I knew that I could find stuff about serialization for C++
                        but I wasn't sure about C. I just searched for

                        +"serialization " +"C programming language"

                        and found lots of stuff but it appears to be more related
                        to Java, Python and C++.

                        If you can find a good serialization library in C,
                        please pass on the pointer.

                        Comment

                        • E. Robert Tisdale

                          #27
                          Re: Writing a structure

                          Barry Schwarz wrote:
                          [color=blue]
                          > E. Robert Tisdale wrote:
                          >[color=green]
                          >>I used Google
                          >>
                          >> http://www.google.com/
                          >>
                          >>to search for
                          >>
                          >> +"C++" +"serialization "
                          >>
                          >>and I found lots of stuff.[/color]
                          >
                          > You actually search on C++ to find stuff about C?[/color]

                          I knew that I could find stuff about serialization for C++
                          but I wasn't sure about C. I just searched for

                          +"serialization " +"C programming language"

                          and found lots of stuff but it appears to be more related
                          to Java, Python and C++.

                          If you can find a good serialization library in C,
                          please pass on the pointer.

                          Comment

                          • Dan Pop

                            #28
                            Re: Writing a structure

                            In <lnvfkdc587.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:
                            [color=blue]
                            >Dan.Pop@cern.c h (Dan Pop) writes:[color=green]
                            >> In <eb99b94a.04040 52318.582c66f8@ posting.google. com>
                            >> glenmendez@yaho o.co.in (Glen) writes:
                            >>[color=darkred]
                            >> >Is it possible to write a structure to a file in c...as in c++...??
                            >> >is it using fwrite??[/color]
                            >>
                            >> There is more than one way of doing it. The two most popular approaches
                            >> are:
                            >>
                            >> 1. Use a text file, and convert each field of the structure to a textual
                            >> representation, using fprintf.
                            >>
                            >> 2. Use a binary file and dump the binary representation of the
                            >> structure value with fwrite.
                            >>
                            >> The first approach is more portable (the value can be read on a different
                            >> platform, or even with a program written in a different language), but
                            >> uses more disk space and CPU cycles.
                            >>
                            >> The second approach uses less disk space and CPU cycles, but the resulting
                            >> binary file can be read only by a C program compiled with the same
                            >> compiler, on the same platform.[/color]
                            >[...]
                            >
                            >You're not guaranteed that the binary file can be read by a C program
                            >compiled with a different compiler or on a different platform, but
                            >*sometimes* it can be, if you're very careful and/or very lucky.
                            >
                            >For different compilers on the same platform, compiler writers
                            >typically use the same layout algorithms to allow data portability.
                            >(This is not guaranteed; it's something you need to verify.)[/color]

                            You can do *anything* you want, if, instead of relying on the language
                            specification, you rely on your own checking that it works in a particular
                            set of circumstances, so I fail to see your point.

                            If you want to write code that works *by design*, you MUST follow my
                            guidelines above, period.

                            Dan
                            --
                            Dan Pop
                            DESY Zeuthen, RZ group
                            Email: Dan.Pop@ifh.de

                            Comment

                            • Dan Pop

                              #29
                              Re: Writing a structure

                              In <lnvfkdc587.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:
                              [color=blue]
                              >Dan.Pop@cern.c h (Dan Pop) writes:[color=green]
                              >> In <eb99b94a.04040 52318.582c66f8@ posting.google. com>
                              >> glenmendez@yaho o.co.in (Glen) writes:
                              >>[color=darkred]
                              >> >Is it possible to write a structure to a file in c...as in c++...??
                              >> >is it using fwrite??[/color]
                              >>
                              >> There is more than one way of doing it. The two most popular approaches
                              >> are:
                              >>
                              >> 1. Use a text file, and convert each field of the structure to a textual
                              >> representation, using fprintf.
                              >>
                              >> 2. Use a binary file and dump the binary representation of the
                              >> structure value with fwrite.
                              >>
                              >> The first approach is more portable (the value can be read on a different
                              >> platform, or even with a program written in a different language), but
                              >> uses more disk space and CPU cycles.
                              >>
                              >> The second approach uses less disk space and CPU cycles, but the resulting
                              >> binary file can be read only by a C program compiled with the same
                              >> compiler, on the same platform.[/color]
                              >[...]
                              >
                              >You're not guaranteed that the binary file can be read by a C program
                              >compiled with a different compiler or on a different platform, but
                              >*sometimes* it can be, if you're very careful and/or very lucky.
                              >
                              >For different compilers on the same platform, compiler writers
                              >typically use the same layout algorithms to allow data portability.
                              >(This is not guaranteed; it's something you need to verify.)[/color]

                              You can do *anything* you want, if, instead of relying on the language
                              specification, you rely on your own checking that it works in a particular
                              set of circumstances, so I fail to see your point.

                              If you want to write code that works *by design*, you MUST follow my
                              guidelines above, period.

                              Dan
                              --
                              Dan Pop
                              DESY Zeuthen, RZ group
                              Email: Dan.Pop@ifh.de

                              Comment

                              • Flash Gordon

                                #30
                                Re: Writing a structure

                                On 7 Apr 2004 16:08:03 GMT
                                Dan.Pop@cern.ch (Dan Pop) wrote:
                                [color=blue]
                                > In <lnvfkdc587.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org>
                                > writes:
                                >[color=green]
                                > >Dan.Pop@cern.c h (Dan Pop) writes:[color=darkred]
                                > >> In <eb99b94a.04040 52318.582c66f8@ posting.google. com>
                                > >> glenmendez@yaho o.co.in (Glen) writes:
                                > >>
                                > >> >Is it possible to write a structure to a file in c...as in
                                > >> >c++...?? is it using fwrite??
                                > >>
                                > >> There is more than one way of doing it. The two most popular
                                > >> approaches are:
                                > >>
                                > >> 1. Use a text file, and convert each field of the structure to a
                                > >> textual representation, using fprintf.
                                > >>
                                > >> 2. Use a binary file and dump the binary representation of the
                                > >> structure value with fwrite.
                                > >>
                                > >> The first approach is more portable (the value can be read on a
                                > >> different platform, or even with a program written in a different
                                > >> language), but uses more disk space and CPU cycles.
                                > >>
                                > >> The second approach uses less disk space and CPU cycles, but the
                                > >> resulting binary file can be read only by a C program compiled
                                > >> with the same compiler, on the same platform.[/color]
                                > >[...]
                                > >
                                > >You're not guaranteed that the binary file can be read by a C program
                                > >compiled with a different compiler or on a different platform, but
                                > >*sometimes* it can be, if you're very careful and/or very lucky.
                                > >
                                > >For different compilers on the same platform, compiler writers
                                > >typically use the same layout algorithms to allow data portability.
                                > >(This is not guaranteed; it's something you need to verify.)[/color]
                                >
                                > You can do *anything* you want, if, instead of relying on the language
                                > specification, you rely on your own checking that it works in a
                                > particular set of circumstances, so I fail to see your point.
                                >
                                > If you want to write code that works *by design*, you MUST follow my
                                > guidelines above, period.[/color]

                                There is one other option. You convert the data in to your own
                                standardised binary format and use that. Of course, depending on the
                                data types you need to output and whether you are concerned about
                                implementations with bytes larger than 8 bits this can be non-trivial.
                                --
                                Flash Gordon
                                Somtimes I think shooting would be far too good for some people.
                                Although my email address says spam, it is real and I read it.

                                Comment

                                Working...