如何在c中获取文件长度? [英] How to get a file length in c?

查看:145
本文介绍了如何在c中获取文件长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个关于如何获取包含十六进制数字的文件长度的快速问题。
例如:

I just have a quick question about how to get the length of a file containing hexadecimal numbers. For example:

724627916C

我能想到的唯一方法是将十六进制值转换为二进制值:

The only way I can think is to convert hex value to binary:

724627916C => 0111001001000110001001111001000101101100

然后计算二进制值的位数。
只是想知道这是否正确?谢谢

then count the bits of binary value. Just wondering if this is the correct way? Thanks

推荐答案

不,
这不是正确的方法。

No, this is not the correct way.

FILE *fp = fopen("filename", "rb");
fseek(fp, 0, SEEK_END);
int lengthOfFile = ftell(fp);
fclose(fp);

原则上:打开文件,寻找结尾并检索当前位置。你会得到的是文件中的字节数。

In principle: Open a file, seek to the end and retrieve your current position. What you'll get is the number of bytes in the file.

编辑:你的问题有点不清楚。是否要检索文件中的字节数,或者文件中十六进制字符串表示的字节数?如果是后者,并且文件中没有任何空格,只需将上述方法返回的字节数除以2即可。

Your question is a little unclear. Do you want to retrieve the number of bytes in the file, or the number of bytes represented by the hexadecimal string in the file? If the latter is the case, and you don't have any whitespaces in your file, just divide the number of bytes returned by the method above by 2.

这篇关于如何在c中获取文件长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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