HELP with 'seek'

new topic     » topic index » view thread      » older message » newer message

I'm having a problem in using the 'seek' function. Being new to
Euphoria,
I'm sure that the problem is my own, but, after struggling with it for
days,
I haven't been able to figure out why the 'gets' function seems not to
be
reading from the position (in the file) that the 'seek' pointed to .

I hope someone on the list can explain what I'm doing wrong.

The program and a test data file are below. (The entire program is
included,
since I don't know where in the program the problem originates).

The program is intended to do the following:

1. Read all records in the test data file to verify (a) number of
records,
    (b) number of bytes per record, which should be 99 (including the
line-feed).

2. From the end-of-file position, it is to back up 3 records, read and
process
    the last 3 records.

The problem begins at the comment -- Back up 3 Records --

After read the 10 records, the 'seek' seems to correctly position
correctly at the first byte
of record no. 7. However the 'gets' function  which follows the 'seek '
does not read the file
beginning at the byte positon pointed to by 'seek', but begins reading
at a position 7 bytes
before that point.

The number of bytes (too early) varies with the number of records in the
file (229 record
file causes the 'gets' to begin reading 219 bytes too far back in the
file).

Any suggestions or solutions will be gratefully appreciated.
(Using Windows 98, the Complete Edition of Euphoria,  and the ED editor)

Thanks.

Jim
 

-- Program begins here ---
-- read last 3 records of a file
include get.e
include file.e

with trace
allow_break(1)

sequence
  Characteristics,fullpath,drive,file

integer 
  Loops,FieldNo

object  

  LowerShadow1,Day1Range,Body1,Record,AvgClose

atom     z,sum,EOF,byteno

function getpath() 
--------------------------------------------------------------------

   -- read a text file into a sequence
   --   TEMPORARILY COMMENTED OUT FOR TESTING
   --   drive = "c"
   --   puts(1, "\n Enter Drive Letter: ")
   --   drive = gets(0) -- get the drive letter in a sequence.
 --It has a linefeed attached that will need to be stripped.

   --   puts(1, "\n Enter Directory Name: ")     -- start new line for
better readability
   --   directry = gets(0)                        -- get the directry
name in a sequence
   --   puts(1,"\n")

   --   puts(1, "\n Enter File Name: ")       -- start new line for
better readability
   --   file = gets(0)                       -- get the file name in a
sequence
   --   puts(1,"\n")

   --   fullpath = drive[1..length(drive)-1] & ":\\"

    -- strip the \n and concatenate  the ":\\" to the end of the drive
   --   directry = directry[1..length(directry)-1] & "\\"

   --   fullpath =  fullpath &directry &file[1..length(file)-1]
   -- concatenate the file & dir name to the drive letter and ":\\"
     ---------------------------------------
     --        FOR TESTING ONLY          ---
     fullpath = "C:\\learn\\testdata.txt" --
     ---------------------------------------
   return fullpath

end function 
--------------------------------------------------------------------------

x        = repeat (0,99)
RecLen   = 0 -- should be 99; 98 bytes of data, one byte line feed
marker probably '\n'
RecNo    = 0
true     = 1
false    = 0
len      = 0
AvgClose = 0
Array    = {}
Loops    = 0
Characteristics = {}

fullpath = getpath()

DataFile = open(fullpath,"r")                   -- open for reading
if DataFile = -1 then
   puts(1, "Couldn't Open specified textfile \n")
   abort(1)
end if

-- how many records are in the file?

while true do
   Record = gets(DataFile)
   if atom(Record) then    exit   end if    -- eof

   Date1 = Record[2..11]
   puts(1,"\n Date is ") puts(1,Date1)

   -- verify that all record lengths are what's expected
   Loops += 1
   if Loops < 2000 then      -- tot. recs is lots less--- eof will occur
first
      if sequence(Record) then
  RecLen = length(Record)
  if RecLen != 99 then
     printf(1,"Unexpected Record Length is %5d\n ...Should Be
99...Aborting",RecLen)
     close(DataFile)
     abort(1)
  end if
       end if
   end if

   RecNo += 1
end while  -- at eof

trace(1)

TotRecs     = RecNo
TotBytes    = TotRecs*RecLen

 ------------------------
 -- back up 3 records  --
 ------------------------
Back3       = TotBytes - (3*RecLen)
StartAt     = seek(DataFile,Back3)

if StartAt != 0 then   puts(1, "seek didn't work\n")    abort(1)  end if

byteno = where(DataFile)
? byteno                 -- s/b 693

RecNo = 0
while true do
   Record = gets(DataFile)  -- error --  does not read from correct
positon.

   if atom(Record) then
      exit   -- if Record = -1, at end of file
   end if

   RecNo      += 1

   Date1       = Record[2..11]
   Open1       = Record[15..25]
   High1       = Record[29..39]
   Low1        = Record[43..53]
   Close1      = Record[57..67]
   Volume1     = Record[71..81]
   OpenInt1    = Record[85..97]     -- final quote mark in 98, line feed
in 99

   Open1Val    = value(Open1)
   High1Val    = value(High1)
   Low1Val     = value(Low1)
   Close1Val   = value(Close1)
   Volume1Val  = value(Volume1)
   OpenInt1Val = value(OpenInt1)

   Day1Range   = High1Val[2] - Low1Val[2]

   if Open1Val[2] > Close1Val[2] then
  Body1 = "black"
      UpperShadow1 = High1Val[2]  - Open1Val[2]
      LowerShadow1 = Close1Val[2] - Low1Val[2]
   elsif
      Open1Val[2] < Close1Val[2] then
      Body1 = "white"
      UpperShadow1 = High1Val[2] - Close1Val[2]
      LowerShadow1 = Open1Val[2] - Low1Val[2]
   elsif
      Open1Val[2] = Close1Val[2] then
      Body1 = "Doji"
      UpperShadow1 = High1Val[2]  - Open1Val[2]
      LowerShadow1 = Close1Val[2] - Low1Val[2]
   end if

   printf(1,"Daily Range  = %9.5f\n", Day1Range)
   printf(1,"Upper Shadow = %9.5f\n", UpperShadow1)
   printf(1,"Lower Shadow = %9.5f\n", LowerShadow1)
   puts(1,Body1)
   puts(1,Record)
   printf(1,"Record No. %5f\n",RecNo)

   -- put the record into an array (7x3) (7 fields, 3 days)

   Array = append(Array,Date1)
   Array = append(Array,Open1Val[2])
   Array = append(Array,High1Val[2])
   Array = append(Array,Low1Val[2])
   Array = append(Array,Close1Val[2])
   Array = append(Array,Volume1Val[2])
   Array = append(Array,OpenInt1Val[2])

end while

--trace(1)
for i = 1 to FieldNo*7 by 1 do          -- 7 fields per record
   if i = 1 or i = 8 or i = 15 then     -- fields containing Date
mm\dd\yyyy
      printf(1,"\n%s", {Array[i]})
   else
      printf(1,"%11.5f", Array[i])    -- field width 11, 5 dec places
   end if
end for

--  now let's average the closes
for i = 5 to FieldNo*7 by 7 do         -- 'cuz 7 fields per rec, close
is field 5
    TotClose = TotClose + Array[i]
end for

AvgClose = AvgClose / RecNo
printf(1,"\n%11.5f", AvgClose)        -- field width 11, 5 dec places
close (DataFile)

---------------------------
-- ascii data file follows:
---------------------------
"07/31/2000"," 1437.00000"," 1449.00000"," 1429.80000","
1438.90000","      70442","       375470"
"08/01/2000"," 1442.00000"," 1454.50000"," 1439.00000","
1447.50000","      62190","       376523"
"08/02/2000"," 1447.00000"," 1461.00000"," 1443.10000","
1452.50000","      58432","       374653"
"08/03/2000"," 1437.00000"," 1465.00000"," 1433.00000","
1461.50000","      58007","       376075"
"08/04/2000"," 1467.50000"," 1473.50000"," 1461.00000","
1471.70000","      65827","       379646"
"08/07/2000"," 1474.00000"," 1490.50000"," 1470.50000","
1486.20000","      53438","       378166"
"08/08/2000"," 1482.00000"," 1494.00000"," 1481.50000","
1491.70000","      41723","       377278"
"08/09/2000"," 1497.00000"," 1498.00000"," 1480.00000","
1481.00000","      47953","       374466"
"08/10/2000"," 1482.00000"," 1483.00000"," 1468.00000","
1474.30000","      51867","       375371"
"08/11/2000"," 1470.00000"," 1484.50000"," 1461.00000","
1478.50000","      42623","       376597"
 

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu