473,587 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help reading binary data from files

I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWN LOAD_DIR):
filename = s.path.join(DOW NLOAD_DIR, name)
if os.path.isfile( filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?

thanks

Feb 6 '07 #1
5 2131
On 2007-02-06, jeff <je**@kalikstei n.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWN LOAD_DIR):
filename = s.path.join(DOW NLOAD_DIR, name)
if os.path.isfile( filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?
What are the f.read(2) calls returning?

--
Grant Edwards grante Yow! Hello, GORRY-O!! I'm
at a GENIUS from HARVARD!!
visi.com
Feb 6 '07 #2
En Tue, 06 Feb 2007 19:01:20 -0300, jeff <je**@kalikstei n.comescribió:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWN LOAD_DIR):
filename = s.path.join(DOW NLOAD_DIR, name)
if os.path.isfile( filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Looks fine to me...

--
Gabriel Genellina

Feb 6 '07 #3
On Feb 6, 4:01 pm, "jeff" <j...@kalikstei n.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWN LOAD_DIR):
filename = s.path.join(DOW NLOAD_DIR, name)
if os.path.isfile( filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele

When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?

thanks
Wow, supreme stupidity on my part. It turns out that there were a lot
of zeros at the beginning of the file, and the slowness of the console
just showed me the zero data during the test time of ~ 10 seconds. If
I throw away the zeros, I see my real data....sorry for the time waste

Feb 6 '07 #4
On Feb 7, 9:01 am, "jeff" <j...@kalikstei n.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWN LOAD_DIR):
filename = s.path.join(DOW NLOAD_DIR, name)
if os.path.isfile( filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele

When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?
Looks to me like it should work -- at least until it hits the end of
the first file. What do you expect to happen at the end of the first
file?? Or did you so snippetise the code so that even if the missing
import statements are added back, it still won't get into the 2nd
file?

I suggest a few more print statements, so that you can see what is
happening.
Try something like this (untested):

f = open(filename, 'rb')
print "Opened", filename
while True:
buff = f.read(2)
if not buff: break # EOF
if len(buff) == 1:
print repr(buff), ord(buff)
break
ele = unpack('<h', buff)[0]
print repr(buff), ele

Print this, cut it out, and paste it to the inside of your hat:

*repr() is your friend*

HTH,
John

Feb 6 '07 #5
On Feb 7, 9:34 am, "jeff" <j...@kalikstei n.comwrote:
On Feb 6, 4:01 pm, "jeff" <j...@kalikstei n.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWN LOAD_DIR):
filename = s.path.join(DOW NLOAD_DIR, name)
if os.path.isfile( filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Any quick tips?
thanks

Wow, supreme stupidity on my part. It turns out that there were a lot
of zeros at the beginning of the file, and the slowness of the console
just showed me the zero data during the test time of ~ 10 seconds.
That's a rather severe case of premature emailisation :-)
If
I throw away the zeros, I see my real data....sorry for the time waste
Some further suggestions (not mutually exclusive):
1. redirect the console to a file
2. write more per line
3. download a free or shareware gadget that will show you the contents
of a file in hex and char
4. You may want/need to write yourself a better dumper that's tailored
to the type of files that you are loooking at, e.g.

498: 0031 FONT len = 001e (30)
502: b4 00 00 00 08 00 90 01 00 00 00 00 00 a5 07 01 ?~~~?~??
~~~~~???
518: 56 00 65 00 72 00 64 00 61 00 6e 00 61 00
V~e~r~d~a~n~a~
532: 041e FORMAT len = 001e (30)
536: 05 00 19 00 00 23 2c 23 23 30 5c 20 22 44 4d 22 ?~?
~~#,##0\ "DM"
552: 3b 5c 2d 23 2c 23 23 30 5c 20 22 44 4d 22 ;\-#,##0\
"DM"
566: 041e FORMAT len = 0023 (35)
570: 06 00 1e 00 00 23 2c 23 23 30 5c 20 22 44 4d 22 ?~?
~~#,##0\ "DM"
586: 3b 5b 52 65 64 5d 5c 2d 23 2c 23 23 30 5c 20 22 ;[Red]\-
#,##0\ "
602: 44 4d 22 DM"

HTH,
John

Feb 6 '07 #6

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

Similar topics

3
2811
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed to the printer to standard in. You have to provide yourself the program that does something with that data, so I thought with some quick c/c++ programming that would be no problem. I tried dozens of ways of reading the data from stdin or cin,...
2
1687
by: Brian | last post by:
Hello, I have a text file I'm attempting to parse. There are about 50 fixed width fields in each line / row. For example (shortened for brevity): W1234Somebody East 101110001111010101 E1235Someone Else West 010111001001010101 I'm having problems pulling these fields into structures, in order to be able to access each individually. I am currently opening as a sequential
3
1347
by: abmd.tw | last post by:
dear all : please help me to slove the problem as below code , it can't run . i want to read a file and display. #include <iostream> #include <fstream> #include <cstdlib> #include <assert.h>
9
4046
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I can pass a max of 512 words to it at a time. So I would pass them in chunks of 512 words until the whole file has been processed. I haven't worked with binary files before so I'm confused with how to store the binary file into memory. What sort of...
50
4907
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
1
2538
by: Jón Sveinsson | last post by:
Hello everyone I have been able to read data from binary files to filestrean, the data in the files are structured, what I'm trying to do is to loop through the binary files and add data to my struct, in this file there are lot of records i need to loop trough, first time I read the file, the data is fine but for the rest the data is not correct here is my code for reading the binary files and loop
7
1799
by: Niyazi | last post by:
Hi, I am developing small insurance application using VB.NET and SQL server 2000. My tables in SQL server are: tbl_Customer (stores the custmer information) tbl_CustImage (stores the customer picture and signature) tbl_InsuranceCompanies (stores the Insurance companies information) tbl_InsuranceType (stores the insurance type)
5
2648
by: EP | last post by:
This inquiry may either turn out to be about the suitability of the SHA-1 (160 bit digest) for file identification, the sha function in Python ... or about some error in my script. Any insight appreciated in advance. I am trying to reduce duplicate files in storage at home - I have a large number files (e.g. MP3s) which have been stored on disk multiple times under different names or on different paths. The using applications will...
6
5253
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
2506
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between startdate and enddate, all in the same query.
0
7927
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7857
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6632
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.