Reading text files - why is this still not solved ?

KrisM

Member
Suppose you have a simple text file (read.txt) with this contents:

A
B
C

Important is that the last line is not terminated with a CR or LF character.

Then you read this text file with a simple program:

DEFINE VARIABLE filename AS CHARACTER NO-UNDO.
DEFINE VARIABLE filetext AS CHARACTER NO-UNDO.
define stream sin.


filename = "read.txt".
input stream sin from value(filename).
repeat:
import stream sin filetext.
MESSAGE "input=" filetext
VIEW-AS ALERT-BOX INFO BUTTONS OK.
end.
input stream sin close.

Output of this demo is:

input= A
input= B
input= B

Which is obviously incorrect.
I tested this on OE11.1 and it is still not solved.

(I suppose i will need to increase awareness of this issue with Progress support. ;))
 

rzr

Member
please enlcose your code with CODE tags.. makes it much more readable.... :)

I don't see any issue with code and have test it. I get correct output in my message box A then B then C ( The last line in file is @ C).

But If I add a CF or LR after C then my output will be A,B,C,C - this again is expected behavior.

I'm on 10.1C / Windows - But i'd expect the same result in anything above 10.1C as well....
 

KrisM

Member
Strange, i can consistently reproduce this on different platforms (OE102b and OE11, unix and windows).
Also can you explain, if input is A,B,C (with last line) expected output is A,B,C,C and not A,B,C or A,B,C,<empty line> ?
 

rzr

Member
Also can you explain, if input is A,B,C (with last line) expected output is A,B,C,C and not A,B,C or A,B,C,<empty line> ?

On each iteration of the REPEAT loop your variable filetext will be populated.
So, for 1st iteration - it will be A, 2nd iteration it will be B, 3rd iteration it will be C, 4th iteration acts as End Of file...and the value of filetext will remain @ C.
 

Stefan

Well-Known Member
Strange, i can consistently reproduce this on different platforms (OE102b and OE11, unix and windows).
Also can you explain, if input is A,B,C (with last line) expected output is A,B,C,C and not A,B,C or A,B,C,<empty line> ?

The last line thing is a 'known issue' - various ProKB articles discuss it. An easy work-around (depending on what you are doing) can be to COPY-LOB the file to a longchar and then run thru the linefeed (~n) entries of this right-trimming the entries with ~r. This may have disadvantages when the input file is large.

We use IMPORT UNFORMATTED - and when the AVM thinks its at the end we compare where we are (using SEEK function) with the size of the file, more the SEEK position back and read the final line with READKEY.

This behavior is very un-ABLish.
 

D.Cook

Member
Thanks for the useful info Stefan, we get tripped up by this "known" (often forgotten) issue every now and then.
 
Top