On 05/31/2010 10:56 AM, eskandari wrote:
But when I try to write offset (number) in binary file, it raise
exception below in line "offsetfile.write(offset)"
"TypeError: argument 1 must be string or read-only buffer, not int"
I search the internet, find that all suggest converting number to
string ---with str()---and then write string to file.
But I shouldn't do this. because the above mentioned C++ function,
read file with this assumption that there are numbers in file.
So I want to know, Is there any way to produce an binary file
containing numbers same as the way C++ does?
Well, you have at least two options:
1) use the pack/unpack functions in the "struct" module to
convert a number to a byte representation that you can then write
to a file
2) write the number to the file as a string and then use C++
libraries to parse a number from a string.
In both cases, you have to consider what happens when the number
is outside the bounds of your C++ data-type (you don't mention
what you're using...an int, a long, or "long long"; signed vs.
unsigned). Additionally in the first case, you have to make sure
that your memory-architecture (big-endian vs. little-endian)
matches on both sides; or that you marshal the data through a
pre-defined format (in libraries, commonly called "network"
format). For such reasons, I'd stick with method #2 unless you
have a strong reason not to.
-tkc
--
http://mail.python.org/mailman/listinfo/python-list