Import Text file with no CR on last line

Ewan

New Member
Hi,

I have a text file consisting of 3 lines. The last line does not have a carraige return at the end of it, therefore if I import the 3 lines, I actually get the first line once and the second line twice, the third line is not being imported.

Is there any way within my Progress code I can import the file correctly? I appreciate that the best option would be to ensure that the source file does indeed have the carraige returns on each line but as these are files created by the clients I dont have that control :)

Thanks!
 

samu fish

New Member
You had the answer in your question

I thought of three possible solutions:

(Note, these are examples only - No error checking, typos included, warranty not...) Sorry, this is rather long answer.

1. memptr

Read the whole file into memptr at once and then loop through the file.
It is usable only if you know that the files you read are not too big.

Code:
define variable mem as memptr.
define variable s as character no-undo.
define variable i as integer no-undo.

file-info:file-name= 'your input file goes here'.
/* allocate memory */
set-size(mem)= file-info:file-size.

/* read the whole file */
input from 'your input file here too'.
import mem.
input close.

/* split input file into lines */
s= get-string(mem,1).

/* free memory */
set-size(mem)= 0.

do i= 1 to num-entries(s,chr(10)):
/* 
  : repeated as many times as there are lines 
  : - even for the last one 
*/
end.

2. Add the missing thing

Like you said it would be best to have it there

Code:
output to 'your input file goes here' append.
put control chr(10).
output close.
Simple...

3. I assume You don't know whether it is there or not

There is a way to find out if there is a carriage return at the end or not. This last one is the way I would go myself.

Code:
define stream file.

function lastByte returns integer (filename as character):

  define variable byte as memptr no-undo.
  define variable i as integer no-undo.

  /* allocate memory for one byte */
  set-size(byte)= 1.

  input stream file from value(filename).
  /* move forward to the last byte of file */
  seek stream file to end.
  seek stream file to ( seek(file) - 1 ).

  /* read byte */
  import stream file byte.
  /* integer representation is enough */
  i= get-byte(byte,1).

  /* free memory */
  set-size(byte)= 0.
  
  /* we are done */
  return (i).
end function.

/* main */
&scoped-define file your-input-file-goes-here
if( lastByte('{&file}' <> 10 )then do:
  output to '{&file}' append.
  put control chr(10).
  output close.
end.

/* your current code should go here  */



Ewan said:
Hi,

I have a text file consisting of 3 lines. The last line does not have a carraige return at the end of it, therefore if I import the 3 lines, I actually get the first line once and the second line twice, the third line is not being imported.

Is there any way within my Progress code I can import the file correctly? I appreciate that the best option would be to ensure that the source file does indeed have the carraige returns on each line but as these are files created by the clients I dont have that control :)

Thanks!
 
Top