简体   繁体   English

使用 2 字节缓冲区从二进制文件中读取字节

[英]Reading bytes from binary file with 2 byte buffer

I am currently trying to read a file and calculate the frequencies of 1 byte equivalent numbers (0 to 255).我目前正在尝试读取文件并计算 1 字节等效数字(0 到 255)的频率。 I want to do the same for 2 byte equivalent numbers (0 to 65535)我想对 2 字节等效数字(0 到 65535)做同样的事情

Simplified version of what I have:我所拥有的简化版本:

int length = 256; //any value 256>
long long values[length]
char buffer[length]
int i,nread;

fileptr = fopen("text.txt", "rb");

for (i=0; i<length; i++){ values[i]=0 }
while((nread = fread(buffer, 1, length, fileptr)) > 0){
   for(i=0;i<nread;i++){
      values[(unsigned char)buffer[i]]++;
   }
}

fclose(fileptr);

for(i=0;i<length;i++{ 
   printf("%d: %lld",i, values[i]); 
}

What I am getting now:我现在得到的:

0: 21

1: 27

...

255: 19

What I want:我想要的是:

0: 4

1: 2

...

65535: 3

At the outset, let me correct what you have said.首先,让我纠正你所说的。 As of now you are not printing the frequencies of 2 byte range.到目前为止,您还没有打印 2 字节范围的频率。 In general unsigned char is 1 byte (8 bits) and the results you are getting are also in accordance with what I said 8 bits => 0 <-> 2^8 -1 => 0 <-> 255一般来说unsigned char是 1 个字节(8 位),你得到的结果也符合我所说的8 bits => 0 <-> 2^8 -1 => 0 <-> 255

For getting the frequencies of 16 bits range you can use u_int16_t , Code goes something like this要获得 16 位范围的频率,您可以使用u_int16_t ,代码如下

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main () {
    FILE* fp = NULL;

    /* Open file and setup fp */

    int *freq = (int*) calloc(65536, sizeof(int));

    u_int16_t value;

    for ( ; ; ) {
        if (read(fileno(fp), &value, sizeof(value)) < sizeof(value)) {
            /* Assuming partial reads wont happen, EOF reached or data remaining is less than 2 bytes */
            break;
        }

        freq[value] = freq[value] + 1;
    }

    for (int i = 0; i < 65536 ; i++) {
        printf("%d : %d\n", i, freq[i]);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM