Results 1 to 7 of 7

Thread: Creating a C struct in Python and writing it to a binary file?

  1. #1
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Creating a C struct in Python and writing it to a binary file?

    How?
    Keyboard not found!

    Press any key to continue...

  2. #2
    Join Date
    Aug 2008
    Beans
    Hidden!

    Re: Creating a C struct in Python and writing it to a binary file?

    Why?

    Why not use XML?

  3. #3
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Re: Creating a C struct in Python and writing it to a binary file?

    Quote Originally Posted by pp. View Post
    Why?

    Why not use XML?
    XML? Huh?

    All I know is the syntax and how to make maps for Scorched3D.
    Keyboard not found!

    Press any key to continue...

  4. #4
    Join Date
    Aug 2008
    Beans
    Hidden!

    Re: Creating a C struct in Python and writing it to a binary file?

    There are several strategies you can use for transferring data between different programs or even for reading and writing data by the same program.

    What you are proposing in the thread's title is to use something like the raw format for a program written in one language to read that data in a program written in another language. That's perfectly possible and presumably not overly difficult (once you know what you're doing) but might result in some headaches at the most awkward moments. Think about how you might use your data of an earlier version in a later version of your programs.

    XML, on the other hand, is an instruments which is designed to move arbitrarily complex data between applications in a manner which is as independent of any particular implementation as they could make it.

    If I was you, I'd read up on XML and exchange data between C and Python programs in XML, unless there were some clear objective reasons not to do so.

  5. #5
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Re: Creating a C struct in Python and writing it to a binary file?

    Quote Originally Posted by pp. View Post
    There are several strategies you can use for transferring data between different programs or even for reading and writing data by the same program.

    What you are proposing in the thread's title is to use something like the raw format for a program written in one language to read that data in a program written in another language. That's perfectly possible and presumably not overly difficult (once you know what you're doing) but might result in some headaches at the most awkward moments. Think about how you might use your data of an earlier version in a later version of your programs.

    XML, on the other hand, is an instruments which is designed to move arbitrarily complex data between applications in a manner which is as independent of any particular implementation as they could make it.

    If I was you, I'd read up on XML and exchange data between C and Python programs in XML, unless there were some clear objective reasons not to do so.
    Writing & reading binary data is much easier in C, and a binary file doesn't eat that much memory.

    The files I'm writing are large, several megabits.
    Keyboard not found!

    Press any key to continue...

  6. #6
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: Creating a C struct in Python and writing it to a binary file?

    Hm.. well you could use "struct" module for what you want.

  7. #7
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Creating a C struct in Python and writing it to a binary file?

    Quote Originally Posted by crazyfuturamanoob View Post
    Writing & reading binary data is much easier in C, and a binary file doesn't eat that much memory.

    The files I'm writing are large, several megabits.
    Megabytes is not too big file (unless you run it on a phone).

    Python allows binary input and output: http://www.python.org/doc/2.5.2/tut/node9.html so I do not see where is your problem.

    See also http://www.devshed.com/c/a/Python/Fi...ent-in-Python/

    Of course if your files are supposed to be cross-platform and cointain multibyte values, you have to consider also http://en.wikipedia.org/wiki/Endian - as usually, if you don't use the simplest solutions, problems just snowball
    Last edited by pmasiar; December 14th, 2008 at 05:27 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •