Question 吉光 芳史 · Sep 21, 2016

How to open ms-dos text files

Hi, There

How to use open text file. I have a one file. the file is original file from excels file. After later convert to CSV file.

some cells has include cr+lf  control code. If i open and use from cache. reding time,Cache was wrong line feed point.

how do i edit my source code?

==============================================

s infile="c:\csvfile.csv"

o infile:"r"

1 u infile r line i $zeof<0 c infile q

   f i=1:1:10 s wd(i)=$p(line,",",i)

g 1

==============================================

Comments

Dmitry Maslennikov · Sep 21, 2016

Please forget about old-way programming. We now have so many features out-of-the-box which helps write understandable code quite easy.

set fs=##class(%Stream.FileCharacter).%New()
set fs.Filename="c:\csvfile.csv"
while 'fs.AtEnd {
  set line=$listfromstring(fs.ReadLine(), ",")

  #; in line you will get listbuild for all columns in a row
​}

class %Stream.FileCharacter helps to read text files, which may contain text in different codepages, and line ends.

Or if you need to read some binary data, which should have some conversion on the fly, use %Stream.FileBinary.

0
Evgeny Shvarov  Sep 21, 2016 to Dmitry Maslennikov

In addition to Dmitry's answer I would also add this link with good discussions on loops coding

and this one regarding try catch in Caché ObjectScript.  

0
Marco Blom · Oct 7, 2016

I have tested the code above with a txt file with 3868 lines:

the file looks like this:

this is my code:

The result is only 12 lines, While line length tells the length of a line is 9??? I do not understand this.

Any suggestions or help please, Thanks in advance!

0