文件在球拍中的位置
我想知道 Racket 中是否有某些东西可以操纵输入端口中的文件位置。 我的情况是,我需要使文件指针之类的东西返回到一个位置,以防我读到我不应该读的内容。
例如 “我很困惑。”在文件 test.txt 中。 那么文件指针就在混淆之前。 (我猜) 那么我可以做些什么,让下次读取字符串时,我得到“am”,而不是“.”?
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 "."??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑
peek-string
。它与 read-string 相同,只是返回的字符保留在端口中以供将来读取。Consider
peek-string
for example. It's identical toread-string
, except that the returned characters are preserved in the port for future reads.为了完整起见,有一个
file-position
函数,可以检索当前位置或更改它。但您通常应该避免它,因为它使代码仅在您可以实际更改位置的端口上工作 - 但情况并非总是如此。正如 Yasir 指出的那样,对于所有阅读功能,通常都有一个“窥视”版本,可以在不移动位置的情况下读取内容,这是一个更好的解决方案。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.