[Caché] Read file line by line in CACHE 5.0.21

796 views
Skip to first unread message

Laser Lips

unread,
May 21, 2010, 6:54:55 AM5/21/10
to Caché, Ensemble, DeepSee
Hi reposting as first post did not work.

I cant use the below code in CACHE 5.0.21 because 'ReadLine' is not
implemented.

Set locationOfFileToProcess="c:\cachesys\csp\perinatal\temp
\1612ADMINISTRATOR_NPER.txt"
Kill headers
Set count=1
Set stream=##class(%File).%New(locationOfFileToProcess)
Do stream.Open("R")
While count<10 {
Set headers=stream.Read()
Set count=count+1
}
Do stream.Close()
Kill stream

Can anyone give me an alternative please?

I need line by line reading.

Thanks
Graham

--
InterSystems: Advanced software technologies for breakthrough applications

Caché 2010.1.1 released on April 8, 2010

Laser Lips

unread,
May 21, 2010, 6:55:30 AM5/21/10
to Caché, Ensemble, DeepSee
This is quite urgent ;-)

Graham

kevin furze

unread,
May 21, 2010, 9:16:59 AM5/21/10
to Caché, Ensemble, DeepSee
heres an old classmethod I have used to read multiple lines, and then
return it into an array

ClassMethod ReadFile(sFilename As %String = "", ByRef ReturnArray As
%String = "")
{
#; maximum line length to be expected
#define MAXLENGTH 32000
kill ReturnArray
; set sFilename="C:\vts\FTP Dir\work\W1252.DWN"
set oFile=##class(%File).%New(sFilename)
#; open mode READ,STREAM read until delimiter or maxlength
if oFile.Open("RS") do
. for lNo=1:1 set nLen=$$$MAXLENGTH , sLine=oFile.Read(.nLen)
quit:nLen=-1 do // nLen = -1 means EOF
. . set ReturnArray(lNo)=sLine
do oFile.Close(),oFile.%Close() // close file, close %File instance
quit
}

call it by doing
d ##class(Utils.Files).ReadFile("myFileName.txt", .retArr)

kev

Laser Lips

unread,
May 21, 2010, 10:24:20 AM5/21/10
to Caché, Ensemble, DeepSee
Thanks Kev, I ended up just using stream.Read(32000) which seems to
only read one line at a time anyway.

Graham

C Mehlman

unread,
May 21, 2010, 10:40:40 AM5/21/10
to intersystems...@googlegroups.com
This was orginally written in 5.0 not sure if it still works:

For {
Use FileDesc
Set Line = $$fread(.Err) ; read the line in the file
If Err '= "" { ; If end of file then done
Set Err = $$fclose(FileDesc,0)
Quit
}
If $LENGTH(Line) > 0 {
Set wstr = Line
}
}
}

This is the updated verison that works with 2007,2008, and 2009:

Set filename = "C:\test.txt"
Set stream=##class(%FileCharacterStream).%New()
Set stream.Filename=filename
While 'stream.AtEnd {
Set line=stream.ReadLine()
If $Length(line) > 0 {
Set wstr = line
...
...
}
}



Craig

Alexander Riemer

unread,
May 21, 2010, 3:04:53 PM5/21/10
to Caché, Ensemble, DeepSee
Hi,

On 21 Mai, 16:40, C Mehlman <craigmehl...@gmail.com> wrote:
> This is the updated verison that works with 2007,2008, and 2009:
>
>  Set filename = "C:\test.txt"
>  Set stream=##class(%FileCharacterStream).%New()
>  Set stream.Filename=filename
>  While 'stream.AtEnd {
>         Set line=stream.ReadLine()
>         If $Length(line) > 0 {
>              Set wstr = line
>                      ...
>                      ...
>         }
>  }

Not sure exactly - I can't test it, but the above should work in 5.0.x
also.

Regards
Alex

Mike

unread,
May 21, 2010, 8:58:57 AM5/21/10
to Caché, Ensemble, DeepSee
S FILE=="c:\cachesys\csp\perinatal\temp\1612ADMINISTRATOR_NPER.txt"
O FILE
F I=1:1 U FILE R X(I)

That will create a local array of X where line I of the file is in the
X(I) variable.

I hope that helps...

Mike
> Caché 2010.1.1 released on April 8, 2010- Hide quoted text -
>
> - Show quoted text -

Benjamin Spead

unread,
May 21, 2010, 7:24:43 AM5/21/10
to intersystems...@googlegroups.com
I don't have 5.0.21 docs in front of me but look in the Class Reference Documentation for %Library.FileCharacterStream or something similarly named. You would %New() the object and set the File(name?) property and then use the ReadLine() methods until AtEnd is true.

Good luck!

Laser Lips

unread,
May 21, 2010, 6:26:58 AM5/21/10
to Caché, Ensemble, DeepSee
Hi, I've written some software which reads in a file line by line and
it works on most of out servers except ones running 5.0.21 becasue
%File.ReadLine is not implemented in 5.0.21.

Here is my code.

Set locationOfFileToProcess="c:\cachesys\csp\perinatal\temp
\1612ADMINISTRATOR_NPER.txt"
Kill headers
Set count=1
Set stream=##class(%File).%New(locationOfFileToProcess)
Do stream.Open("R")
While count=1 {
Set headers=stream.Read()
Set count=count+1
}
Do stream.Close()
Kill stream

Can anyone come up with an easy remedy for CACHE 5.0.21 which will
give me easy line by line file reading?
This is kind of urgent.

Ivo Baehr

unread,
May 22, 2010, 5:38:23 PM5/22/10
to Caché, Ensemble, DeepSee
Hi...

I think that you use the wrong class...
The right class is %FileCharacterStream. This class has the readline
method...

Ivo B. Jr...
IBTech IT Solutions
Brazil
Reply all
Reply to author
Forward
0 new messages