FTELL返回不正确的值 [英] ftell returning incorrect value

查看:193
本文介绍了FTELL返回不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪里FTELL将返回不正确的值的问题。我的code,在NetBeans在Linux上报告正确运行的时候,却完全一样的code,在NetBeans运行在Windows(使用MinGW的)错误地报告。文件指针是在BINARY_READ打开的文件。在我的Linux NetBeans中,我跑子程序之后,FTELL报告35.在我的窗户NetBeans中,调用同一个子程序之后,FTELL是3621我通过我的子程序追溯和下面的语句似乎导致此问题:

I am having a problem where ftell is returning an incorrect value. My code, when run in netbeans on linux reports correctly, but the exact same code, running in netbeans on windows (using mingw) reports incorrectly. the file pointer is to a file opened in BINARY_READ. in my linux netbeans, after running my subroutine, the ftell reports 35. in my windows netbeans, the after calling the same subroutine, the ftell is 3621. I traced through my subroutine and the following statement appears to cause the problem:

if (((header_size = getc (fp)) == EOF) || (header_size == 0))

后在1这个语句结果我的Linux NetBeans中,该FTELL(FP)。
但在我的Windows NetBeans中,这句话后FTELL(FP)是3585。

on my linux netbeans, the ftell(fp) after this statement results in 1. but on my windows netbeans, the ftell(fp) after this statement is 3585.

这可能是造成这个问题?

what could be causing the problem?

推荐答案

您需要以二进制方式打开文件:

You need to open the file in binary mode:

fp = fopen(name, "rb");

或相似。你应该得到的总是这样的习惯,因为只有二进制模式在标准C.良好定义的行为在POSIX系统中,二进制和文本(缺省)模式相同的行为,但在Windows,换行需要改写发生在方式弄乱了文件的内容和偏移量。

or similar. You should get in a habit of always doing this, since only binary mode has well-defined behavior in standard C. On POSIX systems, binary and text (default) mode behave the same, but on windows, munging of newlines takes place in a way that messes up file contents and offsets.

这篇关于FTELL返回不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆