C语言 如何从文件中逐字节读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17362046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:49:44  来源:igfitidea点击:

how to read byte by byte from a file

c

提问by Zisis Diamantis

I want to read the 4 first bytes from a binnary file which is a song.wav type. In a .wav file the 4 first bytes must be 52-46-49-49 and I have to read them to check later if they are true.

我想从一个 Song.wav 类型的二进制文件中读取前 4 个字节。在 .wav 文件中,前 4 个字节必须是 52-46-49-49,我必须阅读它们以便稍后检查它们是否正确。

The thing is that I have a compile arror at the fread line which says invalid conversion from "unsigned char" to "void"and initialzing argument 1 of 'size_t fread(void*,size_t,size_t,FILE*)and i dont know what it means.

事情是,我在FREAD线,说:编译arrorinvalid conversion from "unsigned char" to "void"initialzing argument 1 of 'size_t fread(void*,size_t,size_t,FILE*),我不知道这意味着什么。

I saw in a previous topic tha this is the way that fread must be done if i want to read byte by byte. If anyone has any idea of how i can read byte by byte and store them in an array that be great. Thank you.

我在上一个主题中看到,如果我想逐字节读取,这是必须完成 fread 的方式。如果有人知道我如何逐字节读取并将它们存储在一个很棒的数组中。谢谢你。

void checksong(char *argv[]){
    FILE *myfile;
    int i;
    unsigned char k[4];
    myfile=fopen(argv[2],"r");
    i=0;
    for(i=0; i<4; i++){
       fread(k[i],1,1,myfile);
    }
    for(i=0; i<4; i++){
       printf("%c\n", k[i]);
    }                                  
    return ;
}

回答by Elazar

It is a single error:

这是一个单一的错误:

Invalid conversion from unsigned charto void*initializing argument 1 of

size_t fread(void*,size_t,size_t,FILE*)

unsigned charvoid*初始化参数 1 的无效转换

size_t fread(void*,size_t,size_t,FILE*)

It means k[i]is an unsigned char, and not a pointer. You should use &k[i]or k+i.

这意味着k[i]是一个unsigned char,而不是一个指针。您应该使用&k[i]k+i

However, you don't really need to read byte by byte. You can read 4 bytes, no loops involved:

但是,您实际上并不需要逐字节读取。您可以读取 4 个字节,不涉及循环:

fread(k, 4, 1, myfile);

Printing the numbers:

打印数字:

for (i=0; i<4; i++)
   printf("%d\n", k[i]);

回答by rectummelancolique

In order to read exactly one byte and store it into kat index i, you need to provide the address of element i

为了准确读取一个字节并将其存储到k索引处i,您需要提供元素 i 的地址

for(i=0; i<4; i++){
    fread(&k[i],1,1,myfile);
}

However, you'd rather read the whole 4 bytes in one go if you're interested in them 4. So no forloop at all, and just do:

但是,如果您对它们 4 感兴趣,您宁愿一次读取整个 4 个字节。所以根本没有for循环,只需执行以下操作:

fread(k,1,4,myfile);

It is also good practice to test the return code of fread(and any I/O operation for that matter) in case it fails. man freadfor more information.

测试返回代码fread(以及与此相关的任何 I/O 操作)以防万一失败也是一种很好的做法。man fread想要查询更多的信息。

回答by rectummelancolique

You can use just

你可以只使用

char buffer[4];
fread(buffer,1,4,myfile);

回答by Laksith

you need not to use fread()Even the fgetc()works well. Simple coding. SImple as reading a text file.

你不需要使用fread()Evenfgetc()效果很好。简单的编码。就像读取文本文件一样简单。

#include <stdio.h>

int main(int argc, char** argv)
{
    FILE* f = fopen("lak", "rb");
    fseek(f,0,SEEK_END);
    long lsize=0,i=0;
    lsize = ftell(f);
    rewind(f);

    while(i<lsize){
        int first = fgetc(f);
        i++;
        printf("first byte = %x\n", (unsigned)first); //you can do anything with 
                                                      //first now
    }

fclose(f);

return 0;
}