Recipe: Doing something to each line in a file
Last updated at 5:09 am UTC on 30 August 2008
Problem:
You want to process a text file line by line. You don't want to worry about different line end characters in different operating systems.
Solution:
Create an instance of a CrLfFileStream on the file:
file := FileStream fileNamed: 'test.txt'.
[file atEnd] whileFalse:
[line := file nextLine. "Process the line"]
Discussion
The CrLfFileStream does its job in a platform independent way.
See also
Regular Expressions in Squeak if you want to do more complicated things to each line.