I am attempting to read a text file line by line, so i can do processing on
each line. I have some play code to do this (below), but i thought there might be a more sensible way to do it. Currently in i am reading cr terminated lines and skipping the assumed next lf, but i would rather it was tolerant to unix and mac formats. I would also rather use better (if they exist) smalltalk messages. If anyone can offer any suggestions, they would be appreciated. BTW: I am using Dolphin v2 (and v5 for the moment) Thanks. Play code-- f := File open: 'd:\test.txt' mode: #read. rs := FileStream on: f. [rs atEnd] whileFalse: [ s := rs upTo: (Character cr). rs upTo: (Character lf). Transcript show: s; cr. ]. rs close. f close. -- ps: this is posted to c.l.s.dolphin because in my limited experience the smalltalk class library seems quite vendor (and version) specific. |
"Geoffrey King" <[hidden email]> wrote in message
news:3e39993c$0$9168$[hidden email]... > I am attempting to read a text file line by line, so i can do processing on > each line. > > I have some play code to do this (below), but i thought there might be a > more sensible way to do it. Currently in i am reading cr terminated lines > and skipping the assumed next lf, but i would rather it was tolerant to unix > and mac formats. I would also rather use better (if they exist) smalltalk > messages. You might have a look at SequencedStream<<nextLine. Read the method comment, it says it should handle Unix or Windows style files. One tip that might be helpfull to you is the "Show Inherited Methods" command on the method context menu, I like to use that with "Filter Object Methods" to see all the relevent messages supported by a class. There are quite a few methods scattered between the Stream classes, this option helps to see it all in one place. Also you might look at cutting out the use of File, and just going straight for the FileStream, see FileStream class<<read:text:. Chris |
In reply to this post by Geoffrey King
Geoffrey,
> I have some play code to do this (below), but i thought there might > be a more sensible way to do it. Currently in i am reading cr > terminated lines and skipping the assumed next lf, but i would rather > it was tolerant to unix and mac formats. I would also rather use > better (if they exist) smalltalk messages. > > If anyone can offer any suggestions, they would be appreciated. This is using Dolphin 5 (can't really remember what D2.1 will do) fs := FileStream read: 'd:\test.txt'. [fs atEnd] whileFalse: [ Transcript nextPutAll: fs nextLine; cr]. fs close --- It's nearly always easier to open a FileStream directly, rather than going through File. --- The method comment for Dolphin's #nextLine method includes... "N.B. This method works for Unix and Windows line delimiters (that is CR/LF and LF sequences respectively), but not if the line delimiter consists solely of a CR." --- It's advisable to wrap the #close in an #ensure block so that whatever happens in the main code the FileStream is always closed. This is probably most important when testing code (which can easily cause a walkback) that writes to a FileStream but I tend to try to do it for all FileStreams fs := FileStream read: 'd:\test.txt'. [[fs atEnd] whileFalse: [ Transcript nextPutAll: fs nextLine; cr]] ensure: [fs close] -- Ian |
Thanks Ian - works much better this way.
"Ian Bartholomew" <[hidden email]> wrote in message news:AIh_9.4881$9u5.364348@wards... > Geoffrey, > > > I have some play code to do this (below), but i thought there might > > be a more sensible way to do it. Currently in i am reading cr > > terminated lines and skipping the assumed next lf, but i would rather > > it was tolerant to unix and mac formats. I would also rather use > > better (if they exist) smalltalk messages. > > > > If anyone can offer any suggestions, they would be appreciated. > > This is using Dolphin 5 (can't really remember what D2.1 will do) > > fs := FileStream read: 'd:\test.txt'. > [fs atEnd] whileFalse: [ > Transcript > nextPutAll: fs nextLine; > cr]. > fs close > > --- It's nearly always easier to open a FileStream directly, rather than > going through File. > > --- The method comment for Dolphin's #nextLine method includes... > > "N.B. This method works for Unix and Windows line delimiters (that is > and LF > sequences respectively), but not if the line delimiter consists solely of a > CR." > > --- It's advisable to wrap the #close in an #ensure block so that whatever > happens in the main code the FileStream is always closed. This is probably > most important when testing code (which can easily cause a walkback) that > writes to a FileStream but I tend to try to do it for all FileStreams > > fs := FileStream read: 'd:\test.txt'. > [[fs atEnd] whileFalse: [ > Transcript > nextPutAll: fs nextLine; > cr]] ensure: [fs close] > > -- > Ian > > > > |
In reply to this post by Christopher J. Demers
Thanks Christopher - yep i changed it this way..currently i am using v2.1 so
i dont have the sexy UI features. "Christopher J. Demers" <[hidden email]> wrote in message news:b1c8gr$1226f8$[hidden email]... > "Geoffrey King" <[hidden email]> wrote in message > news:3e39993c$0$9168$[hidden email]... > > I am attempting to read a text file line by line, so i can do processing > on > > each line. > > > > I have some play code to do this (below), but i thought there might be a > > more sensible way to do it. Currently in i am reading cr terminated lines > > and skipping the assumed next lf, but i would rather it was tolerant to > unix > > and mac formats. I would also rather use better (if they exist) smalltalk > > messages. > > You might have a look at SequencedStream<<nextLine. Read the method > comment, it says it should handle Unix or Windows style files. One tip that > might be helpfull to you is the "Show Inherited Methods" command on the > method context menu, I like to use that with "Filter Object Methods" to see > all the relevent messages supported by a class. There are quite a few > methods scattered between the Stream classes, this option helps to see it > all in one place. > > Also you might look at cutting out the use of File, and just going straight > for the FileStream, see FileStream class<<read:text:. > > Chris > > > |
Free forum by Nabble | Edit this page |