472,958 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

determining bytes read from a file.

Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---

But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.

Can someone pls advise me regarding this.
Thanks.

Best Regards,
Vineeth.
Dec 13 '07 #1
9 3782
vineeth wrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---

But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
I doubt that. Python doesn't interpret data when reading, and byte-strings
don't have a implicit 0-based length.

So I think you must be doing something different - clearly the above is not
actual code, but something made up for this post. Show us your actual code,
please.

diez
Dec 13 '07 #2
On Thu, 13 Dec 2007 04:04:59 -0800, vineeth wrote:
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---

But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
If you want to deal with bytes better open the file in binary mode.
Windows alters line endings and stops at a specific byte (forgot the
value) otherwise.

Ciao,
Marc 'BlackJack' Rintsch
Dec 13 '07 #3
On Dec 13, 11:04 pm, vineeth <nvine...@gmail.comwrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---

But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.

Can someone pls advise me regarding this.
Thanks.

Best Regards,
Vineeth.
Dec 13 '07 #4
On Dec 13, 5:13 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
vineeth wrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.

I doubt that. Python doesn't interpret data when reading, and byte-strings
don't have a implicit 0-based length.

So I think you must be doing something different - clearly the above is not
actual code, but something made up for this post. Show us your actual code,
please.

diez
Hi,
The program tries to create a C Byte array of HEX data from a binary
input file (for ex : to embed a .png image with the application as an
array),
Here is the program :

"""
python script to create a bit stream of a input binary file.
Usage : bit_stream_creator.py -i input_file -b bytes_to_dump
"""

import sys
from binascii import hexlify
from optparse import OptionParser

if len(sys.argv) != 5:
print "incorrect args, usage : %s -i input_file -b bytes_to_dump" %
(sys.argv[0])
sys.exit(0)

parser = OptionParser()
parser.add_option("-i", "--input", dest="inputfilename")
parser.add_option("-b", "--bytes", dest="bytes")

(options, args) = parser.parse_args()

print "-i",options.inputfilename
print "-b",options.bytes

# open input file
infile = open(options.inputfilename)

# create the member variable name.
mem_var_name = options.inputfilename
mem_var_name = mem_var_name.replace(' ','_')
mem_var_name = mem_var_name.replace('.','_')

outfile_c = open(mem_var_name + ".c","w")
outfile_h = open(mem_var_name + ".h","w")

# read the data.
print " Reading %d bytes..... " % (int(options.bytes))
bytes_reqd = int(options.bytes)
data = infile.read(bytes_reqd)
print "Bytes Read ", len(data)

# convert to hex decimal representation
hex_data = hexlify(data)
i = 0

# Write the c file with the memory dump.
outfile_c.write ( "unsigned char %s[%d] = {\n" %
(mem_var_name,bytes_reqd) )
while i < len(hex_data):
outfile_c.write( "0x%c%c" % ( hex_data[i],hex_data[i+1] ) )
i += 2
if i != len(hex_data):
outfile_c.write(",")
if i % 32 == 0:
outfile_c.write("\n")
outfile_c.write ( "\n};\n" )

# Write the .h file with forward declaration.
cpp_macro = "__"+mem_var_name.upper()+"_H__"
outfile_h.write("#ifndef "+cpp_macro + "\n")
outfile_h.write("#define "+cpp_macro + "\n")
outfile_h.write( "//%s, size %d \n" % (mem_var_name,len(data)) )
outfile_h.write( "extern unsigned char %s[%d];\n" %
(mem_var_name,bytes_reqd) )
outfile_h.write("#endif //"+cpp_macro + "\n")

#close the files.
outfile_c.close()
outfile_h.close()
infile.close()
________

But len(data) never proceeds beyond the NULL character.

Any help and tips is appreciated.

Thanks and Regards,
Vineeth.
Dec 13 '07 #5
On Dec 13, 11:04 pm, vineeth <nvine...@gmail.comwrote:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---

But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.
Python will not stop on reading '\0'. On Windows in text mode (the
default), '\r\n' will be converted to '\n', and it will stop on
reading ^Z aka chr(26) aka '\x1A'. If you don't want that to happen,
use open('somefile', 'rb') to get binary mode.
Dec 13 '07 #6
On Dec 13, 5:27 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Thu, 13 Dec 2007 04:04:59 -0800, vineeth wrote:
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---
But the bytes read is not being printed correctly, I think bytes are
being counted only till the first occurance of '\0' is encountered.
Even though the file is of a very large size, the bytes till the first
'\0' are counted.

If you want to deal with bytes better open the file in binary mode.
Windows alters line endings and stops at a specific byte (forgot the
value) otherwise.

Ciao,
Marc 'BlackJack' Rintsch
Thanks, opening the file in binary mode helped, thanks a lot.
Dec 13 '07 #7
vineeth wrote:
parser.add_option("-b", "--bytes", dest="bytes")
This is an aside, but if you pass 'type="int"' to add_option, optparse
will automatically convert it to an int, and (I think), give a more
useful error message on failure.
--
Dec 13 '07 #8
A couple potential optimizations:
>
# create the member variable name.
mem_var_name = options.inputfilename
mem_var_name = mem_var_name.replace(' ','_')
mem_var_name = mem_var_name.replace('.','_')
mem_var_name = options.inputfilename.replace(' ','_').replace('.','_')
No need to assign it 3 times.
while i < len(hex_data):
outfile_c.write( "0x%c%c" % ( hex_data[i],hex_data[i+1] ) )
i += 2
if i != len(hex_data):
outfile_c.write(",")
if i % 32 == 0:
outfile_c.write("\n")
This could be re-written as such:

for x in xrange(0, len(hex_data), 32):
output_c.write('%s\n' % ','.join(['0x%s'%''.join(['%c'%a for a in
hex_data[x:x+32][i:i+2]]) for i in xrange(0, 32, 2)]
Dec 13 '07 #9
On Dec 13, 7:50 pm, Chris <cwi...@gmail.comwrote:
A couple potential optimizations:
# create the member variable name.
mem_var_name = options.inputfilename
mem_var_name = mem_var_name.replace(' ','_')
mem_var_name = mem_var_name.replace('.','_')

mem_var_name = options.inputfilename.replace(' ','_').replace('.','_')
No need to assign it 3 times.
while i < len(hex_data):
outfile_c.write( "0x%c%c" % ( hex_data[i],hex_data[i+1] ) )
i += 2
if i != len(hex_data):
outfile_c.write(",")
if i % 32 == 0:
outfile_c.write("\n")

This could be re-written as such:

for x in xrange(0, len(hex_data), 32):
output_c.write('%s\n' % ','.join(['0x%s'%''.join(['%c'%a for a in
hex_data[x:x+32][i:i+2]]) for i in xrange(0, 32, 2)]
Thanks everyone for all the suggestions, and the optimizations, I am
still a newbie and not aware of these ,
thanks!
Dec 14 '07 #10

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

Similar topics

10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
10
by: Orion | last post by:
Hey, I was wondering if it was possible to determine if you hit 'EOF' using fseek? I'm using fseek to traverse through the file from start to end and capturing the data into a linked list...
14
by: googler | last post by:
Is there any C library function that returns the size of a given file? Otherwise, is there a way in which file size can be determined in a C program? I need to get this for both Linux and Windows...
1
by: Sirin Azazi | last post by:
I opened an image file (bmp, jpeg etc) using FileOpenDialog and getting its resolution, height and width. Now I want to get type of the file (not looking at the extension, but which is kept inthe...
3
by: ramesh | last post by:
can any one help me to solve this one we need to write a c program, Consider a file of size 5000 bytes. Open the file and read to a buffer of size buf; We have to read 1000 bytes only from...
0
by: David | last post by:
I have a VB.NET 1.1 app that uses FileStream to read files. It opens a file, reads about 4K "header" data bytes from the beginning of it, then does a Seek into the file (the distance of the Seek...
9
by: tubby | last post by:
Silly question, but here goes... what's a good way to determine when a file is an Open Office document? I could look at the file extension, but it seems there would be a better way. VI shows this...
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.