使用 fgets() 读取和存储

发布于 2024-09-19 19:54:53 字数 95 浏览 2 评论 0原文

我正在使用 fgets 从简单文件(例如 txt 文件)读取文本,但是我需要能够跳回到前一行。有没有办法使用 fgets 来做到这一点?或者我应该将文本存储在某种数组/结构中?

I'm using fgets to read in text from simple files such as txt files however I need the ability to jump back to previous lines. Is there anyway to do this using fgets? Or should I just store the text in some sort a array/structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅紫色的梦幻 2024-09-26 19:54:54

fseekfgetposfsetpos 是合适的。 AFAIK,没有“转到 X 行”功能;您必须保存有关每行的一些信息(例如其起始位置),而使用 fseekfsetpos 来移动。

fseek or a combination of fgetpos and fsetpos would be appropriate. AFAIK, there is no "go to line X" function; you'll have to save some information about each line (e.g. its starting position) instead, using fseek or fsetpos to move around.

£冰雨忧蓝° 2024-09-26 19:54:54

您也许可以通过 fseek() 和朋友 ( http://linux.die.net/man/3/fseek)。

但是,将“fseek”函数与文本文件混合(尤其是在读取和写入同一流时)可能会由于换行符的库转换而导致问题。

如果你的记忆不是太紧张,我会选择保存前几行的信息。

更好的是,如果可能的话,检查您的算法/数据结构,这样您就不需要返回。

You may be able to solve your problems with fseek() and friends ( http://linux.die.net/man/3/fseek ).

However, mixing the "fseek" functions with text files (especially if you're reading and writing to the same stream) may cause problems due to the library translation of line breaks.

If you're not too tight on memory, I'd go with saving information from previous lines.

Better yet, if possible review your algorithm/data structure so that you don't need to go back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文