如何在c中获取文件长度?

发布于 2024-08-10 01:55:58 字数 235 浏览 3 评论 0原文

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

724627916C

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

724627916C => 0111001001000110001001111001000101101100

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

围归者 2024-08-17 01:55:58

不,
这不是正确的方法。

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

原则上:打开文件,查找到末尾并检索当前位置。您将得到的是文件中的字节数。

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

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.

Edit: 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.

我做我的改变 2024-08-17 01:55:58

在类似 un*x 的操作系统中,stat 可用于此目的。

In un*x-like operating systems, stat can be used for that purpose.

朱染 2024-08-17 01:55:58

尝试这个答案寻求帮助。如果这不起作用,Google 有大量关于确定 C 语言文件大小的信息。

Try this answer for some help. If that doesn't work, Google has plenty of information on determining file size in C.

旧人哭 2024-08-17 01:55:58

如果您想计算位数,那么设置一个数组会更简单,该数组告诉您每个十六进制数字(按字符代码索引)中设置了多少位,然后将其相加,而无需显式转换为二进制。

假设C99:

static bits_map[256] =
{
    ['0'] = 0, ['1'] = 1, ['2'] = 1, ['3'] = 2,
    ['4'] = 1, ['5'] = 2, ['6'] = 2, ['7'] = 3,
    ['8'] = 1, ['9'] = 2,
    ['a'] = 2, ['b'] = 3, ['c'] = 2, ['d'] = 3,
    ['e'] = 3, ['f'] = 4,
    ['A'] = 2, ['B'] = 3, ['C'] = 2, ['D'] = 3,
    ['E'] = 3, ['F'] = 4,
};

size_t n_bits = 0;
int c;
while ((c = getchar()) != EOF)
    n_bits += bits_map[c];

printf("Number of bits set: %zd\n", n_bits);

If you want to count the bits, it would be simpler to set up an array that tells you how many bits are set in each hex digit (indexed by character code), and then add things up without explicitly converting to binary.

Assuming C99:

static bits_map[256] =
{
    ['0'] = 0, ['1'] = 1, ['2'] = 1, ['3'] = 2,
    ['4'] = 1, ['5'] = 2, ['6'] = 2, ['7'] = 3,
    ['8'] = 1, ['9'] = 2,
    ['a'] = 2, ['b'] = 3, ['c'] = 2, ['d'] = 3,
    ['e'] = 3, ['f'] = 4,
    ['A'] = 2, ['B'] = 3, ['C'] = 2, ['D'] = 3,
    ['E'] = 3, ['F'] = 4,
};

size_t n_bits = 0;
int c;
while ((c = getchar()) != EOF)
    n_bits += bits_map[c];

printf("Number of bits set: %zd\n", n_bits);
爱人如己 2024-08-17 01:55:58

C 中的文件长度使用函数 _filelength()< /a>

#include <io.h>

int fn, fileSize;
FILE * f = fopen(&String, "rb");
if (f)
{
  fn = _fileno(f);
  fileSize = _filelength(fn);
}
fclose(f);

File length in C goes use the function _filelength()

#include <io.h>

int fn, fileSize;
FILE * f = fopen(&String, "rb");
if (f)
{
  fn = _fileno(f);
  fileSize = _filelength(fn);
}
fclose(f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文