多次打开和关闭文本文件并从延续中阅读
我有一个文本文件,然后将其打开并阅读其中一行,然后关闭文本文件。我在循环下调用我的功能,但是每次此函数都读取文本文件的第一行
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个文本文件,然后将其打开并阅读其中一行,然后关闭文本文件。我在循环下调用我的功能,但是每次此函数都读取文本文件的第一行
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您 can 使用
fseek
在关闭和重新打开后将自己重新定位在文件中,但是这样做是非常不寻常的。实际上,如此不寻常,以至于我认为这是完全错误的。这是一些示例代码,演示了如何执行此操作以及更典型的循环。这里的每个循环都会读取文件的前2行,假设每行都足够小。处理长行超出了这个问题的范围。注意:(1)它是读者的练习。将其作为一种练习也许有点像是有点淘汰,但是处理这些问题的烦恼强烈指向使用标准成语的原因。如果要打印文件的前两行,请使用
的某些变体(int count = 0;(c = fgetc(fp))!= null&& count< 2;){如果(c =='\ n')count += 1; putchar(c); }
。将读取功能作为循环的条件(几乎)总是最好的选择。You can use
fseek
to reposition yourself in the file after closing and reopening, but it is very unusual to do so. So unusual, in fact, that I would suggest it is completely wrong. Here's some sample code that demonstrates how to do that, as well as a more typical loop. Each loop here reads the first 2 lines of the file, assuming each line is sufficiently small; handling long lines is beyond the scope of this question.Notes: (1) It is left as an exercise for the reader how best to handle short reads (eg, when fgets returns NULL) or long lines (eg, when fgets completely fills the buffer but fails to read an entire line). Perhaps it is a bit of a cop-out to leave that as an exercise, but the annoyance of dealing with those issues points strongly towards reasons for using the standard idiom. If you want to print the first two lines of the file, use some variation of
for( int count = 0; (c = fgetc(fp)) != NULL && count < 2; ) { if( c == '\n' ) count += 1; putchar(c); }
. Putting the read function as a condition of the loop is (almost) always the best choice.这些评论已经就您正在尝试的其他替代方案提出了建议。但是,无论是正确的方法,您所陈述的问题似乎很清楚,这很清楚要使用
fseek()
et。在打开和关闭文件时查看连续的行。要打开和关闭文件,每次访问并显示连续的行,您必须首先知道要查看的每个位置都位于该文件中。确实,正如您已经标记的那样, fseek(fseek() =“ https://www.tutorialspoint.com/c_standard_library/c_function_ftell.htm” rel =“ nofollow noreferrer”> ftell())可以使用。以下伪代码步骤说明了一种可能性:
然后...
更完整
The comments have already made suggestions on other alternatives for what you are attempting. But regardless whether it is the right approach or not, it seems pretty clear that your stated ask is clear about wanting to use
fseek()
et. al to view successive lines when opening and closing a file.To open and close a file, and each time access and display a successive line, you must first know where each of the locations to be viewed are located within that file. Indeed, as you have tagged, fseek(), (as well as ftell()) can be used to do this. The following pseudo code steps illustrate one possibility:
Then...
There is a more complete source and run-time example here