Question
· Mar 5

How to read from file starting from a certain position?

The task for a file on disk is to open it, find a keyword (unique) and then read the file line by line, starting with the line with this keyword, then find the next keyword (also unique) and read from that location etc. I would think ObjectScript classes should be able to do that, but the solution eludes me. I probably need to use FindAt to get a position, but ReadLine does not seem to have a position argument. Any advice?

Product version: Caché 2017.1
Discussion (6)2
Log in or sign up to continue

you are in Caché so this might help:
http://localhost:57772/csp/docbook/DocBook.UI.Page.cls?KEY=GIOD_rmsseqfiles
USE file:position
the equivalent in %Stream,Object is MoveTo 
• method MoveTo(position As %Integer) as %Boolean

Move to this position in the stream. If this suceeds then return true, else return false. Note this implementation is not efficient because it searches from the start of the stream, it can be improved upon in specific subclasses. Note that moving to position 1 will be at the start of the stream, position 2 will be at the second character of the stream, etc.

And then you do your Read or Find..

Hi Anna,

this would iterate through a file line by line checking if the line contains a keyword and then outputting the line. also it will continue outputting until another condition resets the found variable to 0.

    Set stream=##class(%FileCharacterStream).%New()
    Set stream.Filename="c:\myfile.txt"
    set keyword="MyTestWord"
    set found=0
    While 'stream.AtEnd {
        Set line=stream.ReadLine()
        if (line [ keyword) {
            // the line contains the keyword out put line
            w !,line
            set found=1
            continue
        }
        if (found=1) {
            // keyword was previously found so continue outputting line
            w !,line
        }
    }