0
votes

I am wondering whether there is something in Racket to manipulate the file position in an input port. My case is that I need to make the something like file pointer goes back to one position in case I read stuff that I should not have read.

e.g. "I am confused." in the file test.txt. then the file pointer is after confused before . (I guess) So is there something I can do in the way that next time I read-string, I get "am", rather than "."??

2

2 Answers

2
votes

Consider peek-string for example. It's identical to read-string, except that the returned characters are preserved in the port for future reads.

1
votes

For the sake of completion, there is a file-position function that can retrieve the current position or change it. But you should generally avoid it, since it makes the code work only on ports where you can actually change a position -- and that's not always true. As Yasir pointed out, for all of the reading functions there is usually a "peek" version that reads something without moving the position, and that's a better solution.