本文介绍了文本模式fseek / ftell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我最近遇到了一个问题。与文本文件和ftell / fseek相关, 我想知道它是否是一个bug,或者只是一个烦人的,但仍然符合b $ b符合,实现。 该平台是Windows,其中文本文件使用CF + LF(0x0d,0x0a)来标记行尾。标记行尾。但是,有问题的文件是Unix格式的, ,每行末尾只有LF(0x0a)。 首先,做上述情况已经调用实施定义 或undefined行为?或者它仍然是定义的? 问题在于ftell()如何报告当前位置。 (并且, 随后fseek()回到同一位置是错误的。) 假设你有fread()以下12个字符,开始在 文件的开头: ''1''''''''''''''''''''''''''''' ''0x0a''1'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ,一个0x0a用于结束 行。) 当你现在在文件中偏移12时,ftell()将返回14 , ,因为它假设那些''\\ n''换行符实际上是CR + LF,而且 CR在读取时被删除了。 (如果这个文件是Windows格式, 你读完这12个字符后你真的会在偏移14处。)对于 fread()返回的每个0x0a,ftell( )假设你在文件中提出了两个 字符。 这里的最终结果是后续的fseek()到同一个位置 将是错误的。 那么,我是否通过在 Windows环境中读取Unix文本文件来调用未定义的行为?或者是允许编译器返回错误的 值作为实现定义的一部分。限制?或者是编译器运行时库中的这个 a错误? - + ------ ------------------- + -------------------- + --------- -------------------- + | Kenneth J. Brody | www.hvcomputer.com | | | kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> | + ------------------------- + -------------- ------ + ----------------------------- + 不要给我发电子邮件:< mailto:Th ************* @ gmail.com>I recently ran into an "issue" related to text files and ftell/fseek,and I''d like to know if it''s a bug, or simply an annoying, but stillconforming, implementation.The platform is Windows, where text files use CF+LF (0x0d, 0x0a) tomark end-of-line. The file in question, however, was in Unix format,with only LF (0x0a) at the end of each line.First, does the above situation already invoke "implementation defined"or "undefined" behavior? Or is it still "defined"?The problem comes in how ftell() reports the current position. (And,subsequently fseek()ing back to the same position is wrong.)Suppose that you have fread() the following 12 characters, starting atthe beginning of the file:''1'' ''2'' ''3'' ''4'' ''5'' 0x0a ''1'' ''2'' ''3'' ''4'' ''5'' 0x0a(Remember, this file is in Unix format, with a single 0x0a for end-of-line.)While you are now at offset 12 within the file, ftell() will return 14,because it assumes that those ''\n'' newlines are really CR+LF, and thatthe CR was stripped off when read. (Had this file been in Windows format,you really would be at offset 14 after reading those 12 characters.) Foreach 0x0a returned by fread(), ftell() will assume you have advanced twocharacters in the file.The net result here is that a subsequent fseek() to the same positionwill be wrong.So, have I invoked undefined behavior by reading a Unix text file in aWindows environment? Or is the compiler allowed to return the "wrong"value as part of an "implementation defined" restriction? Or is thisa bug in the compiler''s runtime library?--+-------------------------+--------------------+-----------------------------+| Kenneth J. Brody | www.hvcomputer.com | || kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |+-------------------------+--------------------+-----------------------------+Don''t e-mail me at: <mailto:Th*************@gmail.com>推荐答案 一个示例程序,包含您的期望和发生的事情可能会使 一切都比较清楚。 - Ben。An example program with what you expect and what happens might makeeverything clearer.--Ben. 除了Ben'正确指出有关fseek()和 ftell()限制,你遗漏了一条重要信息, 即你是以文本还是二进制模式打开文件? 如果你在文本模式下打开一个文件,并且它实际上并不包含平台上文本文件的 格式,那么你就是骗你的编译器 及其库函数。如果你骗你的编译器,它将报复 。 - Jack Klein 主页: http://JK-Technology.Com comp.lang.c的常见问题解答 http:// c- faq.com/ comp.lang.c ++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c ++ http://www.contrib.andrew.cmu。 edu /~a ... FAQ-acllc.htmlIn addition to Ben''s pointing out correctly issues about fseek() andftell() limitations, you left out one piece of important information,namely did you open the file in text or binary mode?If you open a file in text mode, and it does not actually contain theformat for text files on your platform, you are lying to your compilerand its library functions. If you lie to your compiler, it will getits revenge.--Jack KleinHome: http://JK-Technology.ComFAQs forcomp.lang.c http://c-faq.com/comp.lang.c++ http://www.parashift.com/c++-faq-lite/alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html < snip> 除了Ben'正确指出有关fseek()和 ftell()限制的问题,你遗漏了一条重要信息,即你以文本或二进制模式打开文件? 如果您以文本模式打开文件,并且它实际上并不包含平台上文本文件的格式,那么您就是骗你的编译器及其库函数。如果你骗你的编译器,就会报复它。<snip> In addition to Ben''s pointing out correctly issues about fseek() and ftell() limitations, you left out one piece of important information, namely did you open the file in text or binary mode? If you open a file in text mode, and it does not actually contain the format for text files on your platform, you are lying to your compiler and its library functions. If you lie to your compiler, it will get its revenge. 啊。如果我知道这一点,我会简化我的回答在 二进制模式下打开它因为从OP报告的翻译可以告诉 该文件作为文本打开。我一直认为图书馆 必须跟踪它在线路结尾时所做的事情(包括本地和外国的b )以保持其ftell / fseek承诺。 在我看来,因为你只能在某个地方找到你之前(通过阅读)的b $ b b。 的标准给出了更强有力的保证:外国格式的文本文件可以像本机文件一样被操纵。如果没有这样做,为什么?难道比我想象的更复杂吗? - Ben。Ah. Had I known this I would have simplified my answer to "open it inbinary mode" since from the translations that the OP reports one can tellthat the file is opened as text. I had always assumed that the libraryhad to keep track of what it had been doing with line endings (bothnative and foreign) in order to keep its ftell/fseek promise.It seems to me that since you can only seek to somewhere you have beenbefore (by reading) that it would have been possible for the standard togive the stronger guarantee: that foreign-format text files can bemanipulated just like native ones. If this was not done, why? Is it morecomplicated than I imagine?--Ben. 这篇关于文本模式fseek / ftell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 17:02
查看更多