Почему первая строка файла не читается здесь - PullRequest
2 голосов
/ 10 мая 2019

Я пытаюсь использовать следующий код для чтения и печати первой строки всех файлов с расширением .st в каталоге:

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

Однако он не работает и выдает следующую ошибку:

$ 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)

Где проблема?Спасибо за вашу помощь.

Редактировать: следующий код из ответа @tucan не работает:

(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
].

Ошибка:

$ 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)

ОК.Решено: FileStream open: (ff name) Работает.

1 Ответ

1 голос
/ 10 мая 2019

Я не вижу нигде в вашем коде открытия файла. Вам нужно сделать что-то подобное в своем блоке, чтобы открыть файл

(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
].
...