Read Text File In Current Directory
Posted: Wed Dec 13, 2017 6:05 pm
When reading from a text file in the same directory as the code doing the reading, most languages allow the user to simply put the name and extension of the file and assume the rest of the path is the same as that of the code. Lisp doesn't seem to have this feature, and half an hour of searching has not yielded a working alternative. As an example, the following code, whose intended function is to simply print the first line of a referenced text file, has the path C:/Users/Owner/Documents/Lisp/Read_Demo/reader.lisp and references a text file with the path C:/Users/Owner/Documents/Lisp/Read_Demo/input.txt:
Upon execution, the following error occurs:
Clearly, Lisp assumes the file I want is in that "LispWorks..." folder, rather than the current directory. Is there any way to achieve the desired functionality without hard-coding the entire path of the file I wish to read from and while still using the OPEN function?
Code: Select all
(let ((in (open "input.txt")))
(read-line in))
Code: Select all
The file #P"C:/ProgramData/Microsoft/Windows/Start Menu/Programs/LispWorks 6.1 Personal/input.txt" does not exist.