LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2004, 10:22 AM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Python and binary files


I need to save bytes, words, dwords, and floats to files using Python in binary format. Then I need to read those values in from the file in C.

Basically I need an "fwrite" for Python, and use "fread" for C. But only "basiaclly".

Help.

P.S. I already wrote functions "FileWriteByte", "FileWriteWord", etc... and they seem to work fine. My only real problem is with "FileWriteFloat". I don't know how to write a floating point number to a file, so I just do a "FileWriteDWord(FloatNum*1000, FileHandle)" and then divide by 1000 in C. So if someone could help me on this then I would be fine. I am just asking abou the others also because I think there might be a better way. Thanks!
 
Old 08-27-2004, 10:47 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I'm by no means a Python expert, but I did something similar for a Python script to export models from Blender into my own custom binary format. Taking a look at my script, I used this code for my "BinaryFileWriter" class:

Code:
class BinaryFileWriter:
    def __init__(self, filename):
        self.__f = file(filename, "wb")
    def WriteByte(self, b):
        self.__f.write(struct.pack('<B', b))
    def WriteUint(self, i):
        self.__f.write(struct.pack('<I', int(i)))
    def WriteString(self, str, size):
        fmt = "<%is" % size
        self.__f.write(struct.pack(fmt, str))
    def WriteFloat(self, f):
        self.__f.write(struct.pack('<f', float(f)))
    def Close(self):
        self.__f.close()
The struct.pack method being one that will get you binary output. I don't remember all the details of struct.pack, so you will probally want to check out the python docs for it as well.
 
Old 08-27-2004, 02:48 PM   #3
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Thumbs up

Hey thats cool. Thanks!

Btw, this is for a Blender script exporter! Thats cool that your a Blender user.

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 10:19 AM
How to convert a float to its binary or hex representation in Python? zero79 Linux - Software 1 08-29-2005 09:30 PM
binary files in C sagar13 Programming 10 10-02-2004 01:29 PM
Binary files sodaforce Linux - Software 1 09-07-2003 05:31 PM
Using binary files KDE4me Linux - Newbie 1 04-24-2003 08:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration