[Newbies] Re: reading lines from textfiles on Linux

Charles D Hixson charleshixsn at earthlink.net
Tue May 16 14:34:32 UTC 2006


Chris Kassopulo wrote:
> Hi Charles,
>
> I had a similar problem last week and Ron
> showed how to use CrLfFileStream.
>
> You have to test the end of line character.
> You don't have to use a counter since the
> whileFalse will loop through the whole file.
>
> This works for me on linux whether the lines
> end in lf or cr/lf.
>
> | aFile myFile myStream line |
> aFile := '/home/ckasso/logfiles-lf/ws000101.log'.
> myFile := (aFile) asFileName.
> Transcript clear.
> myStream := CrLfFileStream fileNamed: myFile.
> [ myStream atEnd ]
>     whileFalse: [
>         line := myStream upTo: Character lf.
>         Transcript show: line; cr.].
> myStream close.
>
> You'll have to adapt it to your situation.
>
> Chris
>   
Hi Chris,
I am befoozled, because that works the same as the other approach, thus:

    | aFile myFile myStream line |
    aFile := 'aising/data/technologies.csv'.
    myFile := (aFile) asFileName.
    Transcript clear.
    myStream := CrLfFileStream fileNamed: myFile.
    [ myStream atEnd ]
        whileFalse: [
            line := myStream upTo: Character lf.
            Transcript show: 'line:: '; show: line; cr.
    ].
    myStream close.

results in:

    line:: 'technology'                                       
    'id'    'name'    'cost1'    'cost2'    'cost3'    'pre1'   
    'pre2'    'pre3'    'danger'    'typeName'    'typeValue'
    1    'Autonomous Vehicles'    40000    1000    0    27    16    0   
    0        0
    2    'Sociology'    10    500    0    0    0    0    0   
    'discover_public'    1000
    3    'Voice Synthesis'    8000    6000    0    32    0    0    0   
        0

...

    40    'Quantum Computing'    30000    20000    0    11    0    0   
    0        0
    41    'unknown'    1000000000    10000000000    0    41    0    0   
    0        0

Note that only the first line of the result begins with "line::", so
it's exactly the same problem as the other.  (OTOH, your code only
required me to change the file name, which was a good check, as it means
I didn't introduce any new errors.)
Interestingly, when I edited the code to:

    | aFile myFile myStream line |
    aFile := 'aising/data/technologies.csv'.
    myFile := (aFile) asFileName.
    Transcript clear.
    myStream := CrLfFileStream fileNamed: myFile.
    [ myStream atEnd ]
        whileFalse: [
            line := myStream upTo: Character lf.
            Transcript show: 'line:: '; show: line; cr.
    ].
    Transcript cr; show: 'LineEndConvention = '; show:
    myStreamdetectLineEndConvention.
    myStream close.
    Transcript cr; show: 'SmalltalkImage current platformName = '; show:
    (SmalltalkImage current platformName).

I got an ending to the routine of:

    41    'unknown'    1000000000    10000000000    0    41    0    0   
    0        0


    LineEndConvention = lf
    SmalltalkImage current platformName = unix

so it's understanding what the line end should be, and the formatting of
the output shows that it's reading the end of lines, but somehow it's
not seeing them when it comes time to read in a single line.


More information about the Beginners mailing list