473,378 Members | 1,405 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,378 software developers and data experts.

Seeing next character in an file

Is there a way to see the next character of an input file object without
advancing the position in the file?

Jul 18 '05 #1
7 4905
On Sun, 27 Jul 2003 01:09:54 +0000, Grumfish wrote:
Is there a way to see the next character of an input file object without
advancing the position in the file?

To do this, you can do the following:

fin = file('myfile')

....

char = fin.read(1)
fin.seek(-1,1) # set the file's current position back a character

You can then write your own subclass of file, if you want, with "peek"
functionality:

class flexfile(file):
def __init__(self, fname, mode='r', bufsize=0):
file.__init__(self, fname, mode, bufsize)

def peek(self, cnt):
data = self.read(cnt)
self.seek(cnt * -1, 1)
return data

def peekline(self):
pos = self.tell()
data = self.readline()
self.seek(pos, 0)
return data



Jul 18 '05 #2
On Sun, 27 Jul 2003 02:58:13 GMT, "Keith Jones" <kj*****@rochester.rr.com> wrote:
On Sun, 27 Jul 2003 01:09:54 +0000, Grumfish wrote:
Is there a way to see the next character of an input file object without
advancing the position in the file?

To do this, you can do the following:

fin = file('myfile')

...

char = fin.read(1)
fin.seek(-1,1) # set the file's current position back a character

Warning, though: this is very iffy on windows unless you have opened the file
in binary. E.g.,
print >> file('ends_in_windows_EOL.txt','w'), 'Ends in windows EOL here:'
Look at it in binary: file('ends_in_windows_EOL.txt','rb').read() 'Ends in windows EOL here:\r\n'

Cooked: file('ends_in_windows_EOL.txt','r').read() 'Ends in windows EOL here:\n'

Now try to seek back past the apparent \n single character and one more (2)
so we can read the ':' f = file('ends_in_windows_EOL.txt')
f.seek(-2, 2)
f.read() '\n'

Hm. That's a representation of reading the last two characters in cooked mode.
Apparently the seek positioned us to read '\r\n', not a cooked ':\n'

Look at the same in binary: f = file('ends_in_windows_EOL.txt', 'rb')
f.seek(-2, 2)
f.read() '\r\n'

The last two are the windows EOL. Seeking -2 in cooked mode is not positioning at ':\n'
as we can see in the binary:
f.seek(-3, 2)
f.read()

':\r\n'

[...]

So if you're going to seek/tell, best to do it in binary, and deal with the platform dependent EOLs.

Regards,
Bengt Richter
Jul 18 '05 #3

[Keith]
def peek(self, cnt):
data = self.read(cnt)
self.seek(cnt * -1, 1)
return data

def peekline(self):
pos = self.tell()
data = self.readline()
self.seek(pos, 0)
return data
[Bengt] if you're going to seek/tell, best to do it in binary, and deal with the platform dependent EOLs.


seek() works perfectly with text-mode files as long as you only seek to
places given to you by tell(). So if Keith's peek() function had used
tell() and then seek()ed (sought()? 8-) back to that point like his
peekline() does, there would be no problem.

--
Richie Hindle
ri****@entrian.com
Jul 18 '05 #4
Grumfish <no****@nowhere.com> wrote in message news:<CD*******************@twister.nyroc.rr.com>. ..
Is there a way to see the next character of an input file object without
advancing the position in the file?


Here is a little class that I use on a little html parser that I
wrote. You may be able to adjust it for your needs:

class Characters:
def __init__(self, String):
self.Characters=String
self.Char = ""
self.index = 0
self.lenght = len(self.Characters)

def GetNextChar(self):
skipchar = 1
while skipchar ==1:
try:
self.Char = self.Characters[self.index]
except IndexError:
self.Char = None
#print "End of File\n"
return None
self.index += 1
if self.Char != "\n":
skipchar = 0
return self.Char

def CheckNextChar(self):
skipchar = 1
StartChar = self.Char
StartIndex = self.index
while skipchar ==1:
try:
self.Char = self.Characters[self.index]
except IndexError:
self.Char = None
#print "End of File\n"
return None
self.index += 1
if self.Char != "\n":
skipchar = 0
self.index = StartIndex
NextChar = self.Char
self.Char = StartChar
return NextChar
Jul 18 '05 #5

[Richie]
seek() works perfectly with text-mode files as long as you only seek to
places given to you by tell(). So if Keith's peek() function had used
tell() and then seek()ed (sought()? 8-) back to that point like his
peekline() does, there would be no problem.
[Bengt] Can you cite a C or C++ standard section that guarantees that seek/tell
will work that way in text mode? (I'm not saying there isn't one, but
I couldn't find one quickly ;-)


ANSI C, ISO/IEC 9899:1990, section 7.9.9.2: "For a text stream, either
offset shall be zero, or offset shall be a value returned by an earlier
call to the ftell function on the same stream and whence shall be
SEEK_SET."

--
Richie Hindle
ri****@entrian.com
Jul 18 '05 #6
On Tue, 29 Jul 2003 09:05:53 +0100, Richie Hindle <ri****@entrian.com> wrote:

[Richie]
seek() works perfectly with text-mode files as long as you only seek to
places given to you by tell(). So if Keith's peek() function had used
tell() and then seek()ed (sought()? 8-) back to that point like his
peekline() does, there would be no problem.


[Bengt]
Can you cite a C or C++ standard section that guarantees that seek/tell
will work that way in text mode? (I'm not saying there isn't one, but
I couldn't find one quickly ;-)


ANSI C, ISO/IEC 9899:1990, section 7.9.9.2: "For a text stream, either
offset shall be zero, or offset shall be a value returned by an earlier
call to the ftell function on the same stream and whence shall be
SEEK_SET."

Thank you. It makes sense that you could retrieve and set an underlying binary position
and decode forward with arbitrary cooking, so long as that cooking either goes character
by character and leaves the binary position to right after what it has used up, or does
the equivalent by proper binary repositioning if it grabs a chunk and doesn't use all of it
for a given cooking portion, so that the next retrieval (tell) will be valid.

I guess I must have been bitten some time in the foggy past, and never recovered trust.
Now I have enough at least to use it, though I will probably still want to write a little
verification into a unit test if I'm in finicky mode. Thanks again ;-)

BTW, is there an ANSI C spec available free on line (or an older, close-enough draft)?

Regards,
Bengt Richter
Jul 18 '05 #7

[Bengt]
BTW, is there an ANSI C spec available free on line
(or an older, close-enough draft)?


Not a complete one that I'm aware of, no, although some pieces exist at
and around http://www.cs.vsb.cz/grygarek/tuox/s...si/Draft_g.txt

I eventually found the seek/tell rule in a book errata at
http://herd.plethora.net/~seebs/c/c_tcr.html then confirmed the standard
and section numbers with the "Normative Changes to ISO/IEC 9899:1990"
document at http://www.lysator.liu.se/c/tc1.html

I can't remember exactly what I googled for to find those documents (and
the query has already fallen off the end of my Google Toolbar's search
history - I think I use Google too much! 8-)

--
Richie Hindle
ri****@entrian.com
Jul 18 '05 #8

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

Similar topics

19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
3
by: Steven Bethard | last post by:
This has probably been answered before, but my Google skills have failed me so far... Is there an os independent way of checking to see if a particular executable is on the path? Basically what...
4
by: AndrewM | last post by:
Hello, I have a SUB which has input variables. If I run it once its okay, but I need to include it in a for....next loop. Can this be done ? for i=0 to 100 call mySub(arr(0,i)) next
10
by: dev_kh | last post by:
Hi, I am facing a weird issue. Here it goes: I am writing a c# app for a client with which I will read some data and generate a .dat file for them from that data. Then the client will read...
4
by: seema_coma | last post by:
I am working under a Linux environment, and I want to "read stream up to next delimiter". It looks like linux doesnt't have a function such as bgets() All i need is, reading characters from...
20
by: Jukka K. Korpela | last post by:
I recently noticed, once again, how the common implementation of italics can really disturb. I'm referring to the common phenomenon that there is by default too little spacing after italics text,...
1
ctrohana
by: ctrohana | last post by:
Hi guys, I need ur help. If I have character strings (data file) as below: X001234 X001345Y002323 X00142 X001567 How do i program, if I want to enter Y002323 to the next line? and how I...
2
by: GISmatters | last post by:
I have unbound checkboxes in a nested gridview to allow multi-selection of "child" rows. For context, the parent gridview rows are for large "reports", the child rows are for various specific files...
7
by: tempest | last post by:
Hi all. This is a rather long posting but I have some questions concerning the usage of character entities in XML documents and PCI security compliance. The company I work for is using a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.