问题描述
嘿,
我想知道是否有可能确定你是否使用
fseek命中''EOF'?我正在使用fseek从头到尾遍历文件
并将数据捕获到链表结构中。但是,我的循环
看起来效果不好 - 它实际上完全摸不着:
while((a = fseek(fp,0, SEEK_CUR))== 0){
//这里的代码
}
对我来说,不要破坏它是非常重要的
游标的当前位置,因为我依赖它来从文本文件中获取数据。我认为
认为循环可以正常工作,因为fseek只会在错误上返回一个
非零整数,但不幸的是情况并非如此。
任何有使用fseek()或其他功能的建议吗?
任何帮助将不胜感激!谢谢。
Hey,
I was wondering if it was possible to determine if you hit ''EOF'' using
fseek? I''m using fseek to traverse through the file from start to end
and capturing the data into a linked list structure. However, my loop
doesn''t seem to work well - it totally fumbles out actually:
while ((a = fseek(fp,0,SEEK_CUR)) == 0){
// code here
}
Its quite important for me not to disrupt the current position of the
cursor since I rely on that to fetch the data from the text file. I
thought that the loop would work fine since fseek only returns a
non-zero integer on an error but unfortunately this is not the case.
Anyone with suggestions with using fseek() or some other function?
Any help would be greatly appreciated! Thanks.
推荐答案
我不认为只要fp是有效的,它就会返回错误/>
表示可搜索文件的FILE指针。确定
eof的正常方法是从文件中读取并查看它是否返回eof指示。
Villy
I wouldn''t expect this would ever return an error as long as fp is a valid
FILE pointer representing a seekable file. The normal way to determine
eof is to read from the file and see if it returns an eof indication.
Villy
如果您正在从头到尾阅读整个文件,为什么
你在使用fseek()吗?只需继续调用fgets()或
fread()或者你正在使用的任何东西来吸收数据,当
你得到文件结尾时函数会告诉你如此[*]由
返回值EOF。
[*] EOF返回值也可以表示实际的I / O
错误而不是文件结束。对于腰带和吊带
确定性,您可以使用feof()或ferror()来计算出EOF值实际意味着什么。
-
If you''re reading the whole file "from start to end," why
are you using fseek() at all? Just keep calling fgets() or
fread() or whatever you''re using to soak up the data, and when
you get to end-of-file the function will tell you so[*] by
returning the value EOF.
[*] The EOF return value can also indicate an actual I/O
error rather than end-of-file. For belt-and-suspenders
certainty, you can use feof() or ferror() to figure out
what the EOF value actually meant.
--
Er*********@sun.com
当然,有这些用途的feof(fp)。
-
Karthik。
Of course, there is feof(fp) for these purposes.
--
Karthik.
这篇关于使用fseek()确定EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!