473,387 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

AW: Write to a binary file

Hy Mike
Thanks for your links, unfortunately they weren't very usefull for my
specific problem.

Hy Grant Edwards
Thanks for your hints.
A simplified test programm to compare the function for opening a file i
used ("file()") and your suggested "os.open()" showed different
behaviour.

My simple testprogramm:

--- START ---
import os

msg = chr(0x02) + chr(0x36) + chr(0x00) + chr(0x01) + chr(0x0a) +
chr(0xb0) + chr(0x77)

f = os.open('/dev/pytest', os.O_RDWR)
os.write(f,msg)
os.close(f)

f = file('/dev/pytest', 'wb')
f.write(msg)
f.close()
--- END ---

The "pytest" device is a very simple device-driver which prints out
(using "printk()") the buffer delivered to the write function in a
hexadecimal format ("Pytest write[buffer in hex format]").

The output was:
--- Start ---
Pytest write02 36 00 01 0a b0 77
Pytest write02 36 00 01 0a
Pytest writeb0 77
--- END ---

Using os.open will work for me, i wasn't aware of the existence of
several file interaces.

Thanks for your help
regards

Aurel
Apr 5 '07 #1
4 5306
On 2007-04-05, Thomi Aurel RUAG A <Au*********@ruag.comwrote:
A simplified test programm to compare the function for opening
a file i used ("file()") and your suggested "os.open()" showed
different behaviour.

My simple testprogramm:

--- START ---
import os

msg = chr(0x02) + chr(0x36) + chr(0x00) + chr(0x01) + chr(0x0a) +
chr(0xb0) + chr(0x77)

f = os.open('/dev/pytest', os.O_RDWR)
os.write(f,msg)
os.close(f)

f = file('/dev/pytest', 'wb')
f.write(msg)
f.close()
--- END ---

The "pytest" device is a very simple device-driver which
prints out (using "printk()") the buffer delivered to the
write function in a hexadecimal format ("Pytest write[buffer
in hex format]").

The output was:
--- Start ---
Pytest write02 36 00 01 0a b0 77
Pytest write02 36 00 01 0a
Pytest writeb0 77
--- END ---
I'm surprised that the normal file object's write method does
that -- especially for a "binary" file. IMO, it's a bug when a
binary file object treats 0x0a differently than other byte
values. But, using the file object to read/write a device is
probably not a good idea because of undefined behavior like
that. File objects also do their own buffering, which I
suspect isn't what you want.
Using os.open will work for me, i wasn't aware of the
existence of several file interaces.
The os.file/open ones are just very thin wrappers around the
corresponding libc routines. The file object contains it's own
buffering and other features that will just get in the way of
what you probably want to do.

--
Grant Edwards grante Yow! .. here I am in 53
at B.C. and all I want is a
visi.com dill pickle!!
Apr 5 '07 #2
Grant Edwards wrote:
On 2007-04-05, Thomi Aurel RUAG A <Au*********@ruag.comwrote:

>A simplified test programm to compare the function for opening
a file i used ("file()") and your suggested "os.open()" showed
different behaviour.

My simple testprogramm:

--- START ---
import os

msg = chr(0x02) + chr(0x36) + chr(0x00) + chr(0x01) + chr(0x0a) +
chr(0xb0) + chr(0x77)

f = os.open('/dev/pytest', os.O_RDWR)
os.write(f,msg)
os.close(f)

f = file('/dev/pytest', 'wb')
f.write(msg)
f.close()
--- END ---

The "pytest" device is a very simple device-driver which
prints out (using "printk()") the buffer delivered to the
write function in a hexadecimal format ("Pytest write[buffer
in hex format]").

The output was:
--- Start ---
Pytest write02 36 00 01 0a b0 77
Pytest write02 36 00 01 0a
Pytest writeb0 77
--- END ---

I'm surprised that the normal file object's write method does
that -- especially for a "binary" file. IMO, it's a bug when a
binary file object treats 0x0a differently than other byte
values. But, using the file object to read/write a device is
probably not a good idea because of undefined behavior like
that. File objects also do their own buffering, which I
suspect isn't what you want.
Why not try to create a file object with bufsize = 0 ?
for ex:
---------
fo = file('/dev/pytest', 'wb', 0)
fo.write(....)
fo.close()
--------

--
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py

Apr 5 '07 #3
En Thu, 05 Apr 2007 11:38:06 -0300, Grant Edwards <gr****@visi.com>
escribió:
On 2007-04-05, Thomi Aurel RUAG A <Au*********@ruag.comwrote:
>The output was:
--- Start ---
Pytest write02 36 00 01 0a b0 77
Pytest write02 36 00 01 0a
Pytest writeb0 77
--- END ---

I'm surprised that the normal file object's write method does
that -- especially for a "binary" file. IMO, it's a bug when a
binary file object treats 0x0a differently than other byte
values.
A write() call on a Python file object gets directly translated into a
fwrite() C runtime library call. Any special handling is made inside that
library.
But, using the file object to read/write a device is
probably not a good idea because of undefined behavior like
that. File objects also do their own buffering, which I
suspect isn't what you want.
I agree - using os.open, os.write etc. appears to be the right thing here.

--
Gabriel Genellina

Apr 5 '07 #4
On 2007-04-05, Thinker <th*****@branda.towrote:
>>--- START ---
import os

msg = chr(0x02) + chr(0x36) + chr(0x00) + chr(0x01) + chr(0x0a) +
chr(0xb0) + chr(0x77)

f = os.open('/dev/pytest', os.O_RDWR)
os.write(f,msg)
os.close(f)

f = file('/dev/pytest', 'wb')
f.write(msg)
f.close()
--- END ---
I just ran that on my system (2.4.3), and both versions do a
signle write.
>>The "pytest" device is a very simple device-driver which
prints out (using "printk()") the buffer delivered to the
write function in a hexadecimal format ("Pytest write[buffer
in hex format]").

The output was:
--- Start ---
Pytest write02 36 00 01 0a b0 77
Pytest write02 36 00 01 0a
Pytest writeb0 77
--- END ---

I'm surprised that the normal file object's write method does
that -- especially for a "binary" file. IMO, it's a bug when a
binary file object treats 0x0a differently than other byte
values. But, using the file object to read/write a device is
probably not a good idea because of undefined behavior like
that. File objects also do their own buffering, which I
suspect isn't what you want.
Why not try to create a file object with bufsize = 0 ?
for ex:
---------
fo = file('/dev/pytest', 'wb', 0)
fo.write(....)
fo.close()
--------
That's worth a try, but I can't get the normal method to fail...

--
Grant Edwards grante Yow! I selected E5... but
at I didn't hear "Sam the Sham
visi.com and the Pharoahs"!
Apr 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kee | last post by:
Hi All, I am trying to write binary data to a file, which is bmp image: Open "d:\temp\test001.bmp" For Binary Access Write As #1 Put #1, 1, strImage Close #1 *** strImage contains binary...
4
by: Jon Hyland | last post by:
Hi all, I'm looking for the fastest way to write (and/or read) binary to a file in VC++. I've been using the fstream object in this way: //unsigned char *pDataOut and long iLength initilized...
1
by: sleepylight | last post by:
I'm running into a strange problem with ostreams's write function. Please see the code below: ofstream save((job->b->get_filename()).c_str(), ios::out | ios::binary); if (!save) { cout <<...
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
3
by: Billy Smith | last post by:
I'm trying to write a little utility that will write some binary data to a file via a javascript and Windows Script Host under Windows XP. The only way to do this that I can find is to convert...
6
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a...
2
by: Thomi Aurel RUAG A | last post by:
Hy I'm using Python 2.4.2 on an ARM (PXA-270) platform (linux-2.6.17). My Goal is to write a list of bytes down to a file (opened in binary mode) in one cycle. The crux is that a '0x0a' (line...
13
by: zach | last post by:
Can someone help me out, I can't figure out what I'm doing wrong to write to a file in binary mode. What's wrong with my code? <?php $fileName = "something.dat"; $string = "This is a...
6
by: aagarwal8 | last post by:
Hi, I am trying to write the contents of a textbox to a file in binary format. My code looks like this... private void btnWriteToFile_Click(object sender, EventArgs e) { FileStream fs =...
1
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.