我试图了解如何在 C 中读写。这会将二进制文件中的条目存储到缓冲区中直到文件结束。
unsigned char *buffer = (char*) malloc (sizeof(char) * SIZE);
FILE *file = fopen(FILEPATH, "rb");
if(file == NULL){
//error
} else {
while(!feof(file)){
fread(&buffer, SIZE*sizeof(char), 1, file);
// Print out buffer (should be different everytime assume I have different numbers in the file)
}
}
或者我必须在某处使用 fseek 吗?
反之亦然,将某些内容写入文件会起作用吗?谢谢
unsigned char *buffer = (char*) malloc (sizeof(char) * SIZE);
FILE *file = fopen(FILEPATH, "wb");
for(int i=0; i<SIZE; i++){
// put something into buffer based on i
fwrite(&buffer, SIZE*sizeof(char), 1, file);
}