Search code examples
filesmalltalkgnu-smalltalk

Why first line of file not being read here


I am trying following code to read and print first line of all files with extension .st in directory:

(Directory working: '.')
allFilesMatching: '*.st' do: [ :ff |
    line := (ff nextLine).    "Read first line only."
    line displayNl.
    ff close. ]

However, it is not working and giving following error:

$ gst firstline3.st 
"Global garbage collection... done"
Object: nil error: did not understand #oldFileNamed:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #oldFileNamed: (SysExcept.st:1448)
optimized [] in UndefinedObject>>executeStatements (firstline3.st:3)
[] in Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:903)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:378)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo: (VFS.st:396)
Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:902)
File(FilePath)>>allFilesMatching:do: (FilePath.st:775)
Directory class>>allFilesMatching:do: (Directory.st:225)
UndefinedObject>>executeStatements (firstline3.st:2)

Where is the problem? Thanks for your help.

Edit: Following code from answer by @tucan is not working:

(Directory working: '.') allFilesMatching: '*.st' do: [ :ff |
   file := FileStream open: ff mode: FileStream read.
   file nextLine printNl.       "I want to print it right away"
   file close
].

The error is:

$ gst firstline3.st 
"Global garbage collection... done"
Object: RecursiveFileWrapper new "<0x7f6decddd780>" error: did not understand #indexOfSubCollection:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
Kernel.RecursiveFileWrapper(Object)>>doesNotUnderstand: #indexOfSubCollection: (SysExcept.st:1448)
FileStream class(FileDescriptor class)>>open:mode:ifFail: (FileDescr.st:131)
FileStream class(FileDescriptor class)>>open:mode: (FileDescr.st:111)
optimized [] in UndefinedObject>>executeStatements (firstline3.st:2)
[] in Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:903)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:378)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo: (VFS.st:396)
Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:902)
File(FilePath)>>allFilesMatching:do: (FilePath.st:775)
Directory class>>allFilesMatching:do: (Directory.st:225)
UndefinedObject>>executeStatements (firstline3.st:1)

Ok. Solved: FileStream open: (ff name) works.


Solution

  • I don't see anywhere in your code opening the file. You need to do something like this in your block in order to open a file

    (Directory working: '.') allFilesMatching: '*.st' do: [ :ff |
       file := FileStream open: (ff name) mode: FileStream read.
       file nextLine. "How you want to print it here or somewhere else?"
       file close
    ].