9

I'm having a bit of an issue taking a text file and converting it into a list or string.

Say I have "blah.txt" which holds:

3 + 4

Now I want to call that file which I know can be done by

(define in (open-input-file "blah.txt"))

Where do I take it from here?

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Ceelos
  • 1,116
  • 2
  • 14
  • 35
  • The leading quote in the question doesn't look right. I expected `(define in (open-input-file "blah.txt"))`. Please confirm. – dyoo May 06 '12 at 00:09
  • Oh yeah, My mistake. didn't mean to put that quotation mark. It should've read (define in(open-input-file "blah.txt")) – Ceelos May 06 '12 at 03:00
  • 1
    Ok, good. I'll edit the question accordingly without the quote. Good luck! – dyoo May 06 '12 at 20:10

2 Answers2

15

Take a look at the file->list function or file->lines, which should do what you want in Racket. That is, something like (file->lines "blah.txt") will give you a list of lines from the file. More generally, look at the guide entry on I/O.

Animesh Pandey
  • 5,900
  • 13
  • 64
  • 130
Asumu Takikawa
  • 8,447
  • 1
  • 28
  • 43
6

Given a file name, file->string loads the file and returns the content as a string.

wedesoft
  • 2,781
  • 28
  • 25