0

指针相关问题。我正在浏览一些示例代码,这些代码当前将数据从名为dataFile的文件读取到缓冲区中。读取在循环内完成,如下所示:

unsigned char* buffer = (unsigned char*)malloc(1024*768*);
fread(buffer,1,1024*768,dataFile);
redPointer = buffer;
bluePointer = buffer+1024;
greenPointer = buffer+768;

现在,我想尝试将数组缓冲区的全部内容写入文件,这样我就可以只保存那些离散的图像(而不是大文件)。但是,我不完全确定如何去做。

我试图计算语句,但是我在控制台上打印出垃圾字符,并且 PC 发出哔哔声。所以我结束了我的程序。

除此以外是否还有其他方法:

for (int i=0; i < (1024*768); i++) {
fprintf(myFile, "%6.4f , ", buffer[i]); 
}
4

1 回答 1

1

By declaring your buffer as a char*, any pointer arithmatic or array indexes will use sizeof(char) to calculate the offset. A char is 1 byte (8 bits).

I'm not sure what you are trying to do with the data in your buffer. Here are some ideas:

Print the value of each byte in decimal, encoded as ASCII text:

for (int i=0; i < (1024*768); i++) {
    fprintf(myFile, "%d , ", buffer[i]);
}

Print the value of each byte in hexadecimal, encoded in ASCII text:

for (int i=0; i < (1024*768); i++) {
    fprintf(myFile, "%x , ", buffer[i]);
}

Print the value of each floating point number, in decimal, encoded in ASCII text (I think my calculation of the array index is correct to process adjacent non-overlapping memory locations for each float):

for (int i=0; i < (1024*768); i += sizeof(float)) {
    fprintf(myFile, "%6.4f , ", buffer[i]);
}

Split the buffer into three files, each one from a non-overlapping section of the buffer:

fwrite(redPointer, sizeof(char), 768, file1);
fwrite(greenPointer, sizeof(char), 1024-768, file2);
fwrite(bluePointer, sizeof(char), (1024*768)-1024, file3);

Reference for fwrite. Note that for the count parameter I simply hard-coded the offsets that you had hard-coded in your question. One could also subtract certain of the pointers to calculate the number of bytes in each region. Note also that the contents of these three files will only be sensible if those are sensibly independent sections of the original data.

Maybe this gives you some ideas.

Updated: so I created a complete program to compile and test the formatting behavior. This only prints the first 20 items from the buffer. It compiles (with gcc -std=c99) and runs. I created the file /tmp/data using ghex and simply filled in some random data.

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

int main()
    {
    FILE* dataFile = fopen("/tmp/data", "rb");
    if (dataFile == NULL)
        {
        printf("fopen() failed");
        return -2;
        }

    unsigned char* buffer = (unsigned char*)malloc(1024*768);
    if (buffer == NULL)
        {
        printf("malloc failed");
        return -1;
        }

    const int bytesRead = fread(buffer,1,1024*768,dataFile);
    printf("fread() read %d bytes\n", bytesRead);

    // release file handle
    fclose(dataFile); dataFile = NULL;

    printf("\nDecimal:\n");
    for (int i=0; i < (1024*768); i++) {
        printf("%hd , ", buffer[i]);
        if (i > 20) { break; }
    }
    printf("\n");

    printf("\nHexadecimal:\n");
    for (int i=0; i < (1024*768); i++) {
        printf("%#0hx , ", buffer[i]);
        if (i > 20) { break; }
    }
    printf("\n");

    printf("\nFloat:\n");
    for (int i=0; i < (1024*768); i += sizeof(float)) {
        printf("%6.4f , ", (float)buffer[i]);
        if (i > 20) { break; }
    }
    printf("\n");

    return 0;
    }
于 2012-08-01T02:32:36.200 回答