C:读取二进制文件到内存中,改变缓冲,缓冲写入到文件 [英] C: read binary file to memory, alter buffer, write buffer to file

查看:167
本文介绍了C:读取二进制文件到内存中,改变缓冲,缓冲写入到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:
打开二进制数据文件,读整个文件到内存中,改变一些地方经常被他的文件,内存缓冲区写入文件,关闭文件。利润是多少?

The goal: Open a file with binary data, read the whole file into memory, change some parts oft he file, write the memory buffer to the file, close the file. Profit?

问题:
我刚开始学习C,我无法找到有关如何更改二进制数据的内存缓冲区足够的信息。从Web开发的背景(PHP,Python和AS3)即将到来,这是一个新的领域对我来说。

The problem: I have just started learning C and I can't find enough information about how to change to binary data in the memory buffer. Coming from a web developer background (php, python, as3) this is new territory for me.

的上下文:
我有一个函数,它接受的文件和一个指针ADRESS到一个字符指针内存缓冲区的路径。然后它打开该文件,通过文件环和到所述存储器缓冲器写入数据。最后关闭该文件。

The context: I have a function which takes the path to the file and a pointer adress to a char pointer memory buffer. It then opens the file, loops through the file and writes data to the memory buffer. Finally closes the file.

二进制文件的目的是持有类的IDS对一些对象和他们的立场是他们自己的ID。该类别的ID是重新psented为2个字节短裤$ P $。所以基本上它只是充满了很多,我希望能够读取和改变短裤二进制文件。

The purpose of the binary file is to hold category-ids for some objects and their position is their own id. The category-ids are represented as 2 byte shorts. So essentially it's just a binary file filled with lots of shorts that I want to be able to read and change.

下面是我走到这一步:

main.c中:

#include "binary-handler.h"

void showFileBuffer(char *buffer, unsigned int fileSize){
    int i = 0;
    for(; i < fileSize; ++i){
        printf("<%d:%x>\n", i, ((char *)buffer)[i]);
    }
}

int main(){
    char path[] = "assets/map-squares.bin";
    char *buffer;
    int fileSize;
    fileSize = readFileToMemory(path, &buffer);
    showFileBuffer(buffer, fileSize);

    //Code to change buffer
    //Code to write buffer to file
    return 0;
}

二进制handler.c:

binary-handler.c:

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

unsigned int getFileSize(FILE **file){
    unsigned int size;
    if(fseek(*file, 0, SEEK_END) == -1){ return -1; }
    size = ftell(*file);
    fseek(*file, 0, SEEK_SET);
    return size;
}

char *getFileBuffer(FILE **file, unsigned int fileSize){
    char *buffer = malloc(fileSize + 1);
    fread(buffer, fileSize, 1, *file);
    return buffer;
}

unsigned int readFileToMemory(char path[], char **buffer){
    unsigned int fileSize;

    FILE *file = fopen(path, "rb");
    if(file != NULL){
        fileSize = getFileSize(&file);
        *buffer = getFileBuffer(&file, fileSize);
        fclose(file);
        return fileSize;
    }else{
        *buffer = NULL;
        return -1;
    }
}

1 这会不会code生产的第一步(读文件到内存)是否正确?

1. Will this code produce the first step (reading file to memory) correctly?

2 如果有,我该怎么改,说在缓冲区第二对象有0F 00的值?

2. If yes, how can I change, say the 2nd object in the buffer to have a value of 0F 00?

3。我怎么能缓冲区并把它写回文件?

3. How can I take the buffer and write it back to the file?

4。有没有我检查的详细方式缓冲区中的值?一个办法

4. Is there a way for me to check the values in the buffer in a verbose way?

所有的一切我只想让整个概念的把握帮助,所以我可以解决这个我自己。

谢谢!

编辑:删除文件的循环。添加了打印整个缓冲区的功能。

Removed the looping of the file. Added a function which prints the whole buffer.

推荐答案

1)号,您不需要环路 getFileBuffer ,因为你读的整个文件 FREAD 。你也不需要调用 fseek的因为每次你从文件中读取的时间,你会自动将文件流中前进。我还没有调试您的code,但它看起来像你的循环结束的时候,在每一个缓冲元素将包含相同的值,这将是等于无论是在你的文件的最后一个字节。

1) No. You do not need to loop in getFileBuffer since you read the entire file with fread. You also do not need to call fseek because every time you read from the file you will advance within the file stream automatically. I haven't debugged your code, but it looks like by the time your loop completes, every element in buffer will contain the same value and it will be equal to whatever is the last byte in your file.

注:为参数指定的fread您是倒退。第二个参数是你正在阅读这应该是的sizeof类型的大小(字符)。第三个参数应该是你想读这应该是档案大小的字符量。您code仍然有效,不过,却是说必须要读 1 对象,它是档案大小字节是很早以前的,当你正在读档案大小对象 1 字节长。

Note: The arguments your specified for fread are backwards. The second parameter is the size of the type you are reading which should be sizeof(char). The third parameter should be the amount of chars you want to read which should be fileSize. Your code still works, though, but it is saying it wants to read 1 object that is fileSize bytes long when you are reading fileSize objects that are 1 byte long.

2)你可以这样写的第二短值(小端):

2) You can read the second short value like this (in little endian):

short n = 0;
n |= buffer[2] << 0;
n |= buffer[3] << 8;

您可以写短回像这样的文件:

You can write the short back to the file like this:

buffer[2] = n >> 0;
buffer[3] = n >> 8;

3) FWRITE

4)我不明白你的要求。

这篇关于C:读取二进制文件到内存中,改变缓冲,缓冲写入到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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