Search code examples
file-ioschememit-scheme

How do I read a text file in MIT/GNU Scheme?


I have been going through SICP and I would like to apply some of the concepts that I have learned so far. Namely accumulate, map and filter will help me be far more productive at work. I work mostly with CSV files and I know that MIT/GNU scheme does not support this file format. But that's ok as I can export the CSV file to txt file since txt files are supported.

Now I read Section 14 Input/Output of the manual and frankly the lack of specific examples is not helping me get started. Therefore I am hoping some of you could give me a head start. I have a text file, foo.txt, consisting of variables and observations for a list of countries. I just want to read this file into Scheme and manipulate the data. Thank you for your help. Any sample code would be helpful.


Solution

  • (call-with-input-file "my_file.txt"
      (lambda (port)
        (read port))) ; reads the file's contents
    

    See the reference manual on file ports and on ports in general.