Re: reading in text files
Re: reading in text files
- Subject: Re: reading in text files
- From: Bob Savage <email@hidden>
- Date: Fri, 01 Feb 2002 12:41:45 -0600
on 2/1/02 5:45 AM, jgo wrote:
>
>
I'll bite. What if the file were 160MB... or 1.6GB?
>
>
How would one have it read in only a continuous series
>
of chunks and have it parse them, while minimizing monkeying
>
around with chunk boundary conditions?
>
Python has a nice mechanism for this. You can do the following:
#
# python code
#
f = open("foo.txt") # f is a file object
line = f.readline() # line is now a string
while line:
# do whatever you want with one line,
# e.g. string splitting, etc.
#
line = f.readline()
f.close()
Of course this assumes the presence of a line ending character, but the
original question probably had something like that. Arbitrary binary files
would be used differently.
#
# python again
#
# read at most 1,048,576 bytes
data = f.read(1048576)
# move the cursor to the point 1,048,576 bytes
# from the end of the file
f.seek(1048576, 2)
# and so on
At any rate, something like this could be created as a class in Obj-C by
using the ANSI C parts of it. It would be nice to have a class like this
already available as part of Cocoa, though.
Bob