Re: HELP with 'seek'
- Posted by Irv Mullins <irv at ELLIJAY.COM> Aug 18, 2000
- 495 views
On Fri, 18 Aug 2000, you wrote: > Jim writes: > > I'm having a problem in using the 'seek' function... > > ...DataFile = open(fullpath,"r") -- open for reading > > When you use seek(), you have to keep track of > byte offsets within a file very carefully. > Opening a file as "r" (read in text mode) is going to > cause you no end of trouble. It would be better to > open the file as "rb" (read in binary mode) and realize > that each line of text (each record) > normally ends with two characters: \r\n. > > I believe seek() looks at all the characters in the file, > whereas gets() will only "see" \n, not \r\n as is actually > stored in the file. > > The division of I/O on DOS/Windows into "text" vs. > "binary" modes was completely unnecessary, and > has caused confusion for many years. I guess it > was a quick kludge to make some IBM printer > work better, back in the early 80's. In addition, it may be easier to use get() to read these lines in. This will allow for format variations and eliminate the need to cast strings into values. Depending upon how many lines are in the actual data you're working with, it would probably be much easier to just load them all into a sequence, and work with them there. See code below. Beyond that, I note that (1) the sample data only contains 86 characters plus a line feed (except for the last line) no 99 that your code is looking for. Maybe your e-mail client (or mine) clipped the lines? Regards, Irv include get.e constant DATE = 1, OPEN1 = 2, HIGH1 = 3, LOW1 = 4, CLOSE1 = 5, VOLUME1 = 6, OPENINT1 = 7 object data, line atom fn function readline() object line, item line = {} for i = DATE to OPENINT1 do item = get(fn) if item[1] = GET_SUCCESS then line = append(line,item[2]) else return -1 end if end for return line end function fn = open("testdata.txt","r") data = {} while 1 do line = readline() if atom(line) then exit else data = append(data,line) end if end while close(fn) printf(1,"%d records read\n",length(data)) for i = 1 to length(data) do printf(1,"Date: %s\n",{data[i][DATE]}) end for