Re: saving Excel data as a file to read in program
Craig Welch wrote:
> Derek Parnell wrote:
<snip>
> Excel saves text files with CR *and* NL.
This is the standard on DOS and Windows. But on Linux, only '\n' is used
to denote a new line.
Does that mean that all our programs, which read and write text files,
are not cross-platform compatible, if we don't explicitly take care of
'\r' and '\n' anywhere? Fortunately, no! Euphoria handles this for us.
In %EUDIR%/HTML/lib_h_o.htm#open (Eu 2.4) it reads:
"Output to *text* files will have carriage-return characters
automatically added before linefeed characters. On input, these
carriage-return characters are removed."
> Might I suggest changing the routine as follows?
In the following code, Derek used
open(..., "r")
i.e. he opened the file in *text* mode. So the above considerations
apply. Derek's code is correct IMHO.
> }}}
<eucode>
> global function ReadTextFile(sequence pFileName)
> sequence lLines
> object lNextLine
> integer fh
>
> lLines = {}
> fh = open(pFileName, "r")
> if fh = -1 then
> return -1
> end if
>
> lNextLine = gets(fh)
> while sequence(lNextLine) do
> -- if lNextLine[length(lNextLine)] = '\n' then
> -- Don't include any trailing newline.
> -- lLines = append(lLines, lNextLine[1..length(lNextLine)-1])
> -- else
> -- lLines = append(lLines, lNextLine)
> -- end if
>
> --******************** Added CW 9/9/4 ******************
> while lNextLine[length(lNextLine)] = '\n' or
> lNextLine[length(lNextLine)] = '\r' do
> lNextLine = lNextLine[1..length(lNextLine)-1]
> end while
> lLines = append(lLines, lNextLine)
> --******************************************************
>
> lNextLine = gets(fh)
> end while
>
> close(fh)
>
> return lLines
> end function
> </eucode>
{{{
Regards,
Juergen
--
A: Because it considerably reduces the readability of the text.
Q: Why?
A: Top posting.
Q: What is annoying in e-mail and news?
|
Not Categorized, Please Help
|
|