Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Article Evgeny Shvarov · May 11, 2016 1m read

The simplest snippet to read from file in InterSystems IRIS

Hi!

I believe the simplest is (to work with csv delimited by ";"):


set file = ##class(%File).%New( "data.csv" )
    set sc = file.Open( "R" ) 
    if $$$ISERR(sc) quit    ; or do smth

    while 'file.AtEnd {
        set str=file.ReadLine() 
        for i=1:1:$length( str, ";" ) {
            set id=$piece( str, ";" ,i ) 
            write !, id  // or do smth
        }
    }
    do file.Close()

Possible options:

different variants of error handling with sc code.

Embrace while loop into try/catch block.

And what's yours?

Comments

Timur Safin · May 11, 2016

Ok, here is the "simplest"

N IO,D,P,I,A,X,L S IO=$I R D U 0 W !,D,! S P="^" F  U IO R A Q:A=""  I $P(A,P,3) S L=$P(A,P,5) S:L X=$ZU(55,L) ZL  ZS @$P(A,P) S:L X=$ZU(55,0) U 0 W !,$P(A,P,1,2),?20," loaded" ;(Self-loading)

(Well, I'm joking - this is the standard preamble for self-loading INT file)

0
Nikola Borkowski  Apr 21, 2020 to Timur Safin

maybe enigmic,but i think it's very convient and good-short!

0
Nikola Borkowski  Apr 21, 2020 to Timur Safin

it's like my native tatarian tongue))))))

0
Mark Hanson · May 11, 2016

I would generally advise against using %File class for reading files as this is a very low level wrapper around the COS file commands. For example if you want to apply a translate table you need to know about the 'K' flag on the open command. I much prefer using the %Stream.FileBinary or %Stream.FileCharacter classes as these provide a much more consistent interface to the file.

0
Mike Kadow · May 12, 2016

Evgeny,

Thank you for sharing this snippet.

However, I don't understand why you de-piece a line with a delimiter of ";"?

I would just like to see the line like it is.

What am I missing here?

0
Timur Safin  May 12, 2016 to Mike Kadow

You have missed the extension of file "data.csv" used in the example. CSV stands for "Comma Separated Values", which is simplest way to exportt Excel like data to the textual file. And in this case it was apparently exported in the Russian-loale where "comma" is ";" actually.

0
Evgeny Shvarov  May 12, 2016 to Mike Kadow

Yes) Timur already answered. This snippet is not very general "read from file" snippet - but snippet to parse "russian-like" csvs) But every time when I work with text files line by line I use it. 

0
Mike Kadow  May 12, 2016 to Evgeny Shvarov

Thank you for that clarification, but, should that be part of the opening documentation?

0
Evgeny Shvarov  May 12, 2016 to Mike Kadow

Didn't get it. You mean I should change the description for the snippet?

Or to change the snippet to make it less "csv" specific?

0
Evgeny Shvarov  May 12, 2016 to Stefan Wittmann

Oh, thanks for this! I think it worth to make a separate snippet posting how to use the record mapper. Haven't found it in the documentation.

0
Evgeny Shvarov  May 12, 2016 to Stefan Wittmann

And since Yesterday here is new fashion way to make links to documentation ;)

0
Mike Kadow · May 12, 2016

Sorry, I am not criticizing what you have done.

I had to ask why you de-pieced the line based on ";"

and I was given an answer.

Suppose I did not ask, took your snippet and assumed to worked for all files?

All I am saying is the assumption you used (Russian style files) should be part of your documentation so someone not familiar with these style of files would know. Any (unfamiliar) assumptions should be documented. 

0
Evgeny Shvarov  May 12, 2016 to Mike Kadow

Agreed. Will fix it. Anyway, you are very welcome to add your version of the simplest ever file management snippet ;)

0
Mike Kadow · May 12, 2016

Stefan,

Thanks for the tip on the Record Mapper, great stuff!

0