ANSI C我对fwrite和fread做错了什么?

时间:2022-08-25 18:52:50

I have the following code in which I attempt to write some data to a file on first execution, and then on second execution reads the file and prints the data to the screen.

我有以下代码,我尝试在第一次执行时将一些数据写入文件,然后在第二次执行时读取文件并将数据打印到屏幕上。

In the first execution it writes the data to the screen exactly as it is in the code (test_data), and the file is created as expected.

在第一次执行时,它将数据完全按照代码(test_data)写入屏幕,并按预期创建文件。

The problem is when I read the file back and write what I have read to the screen it is just the plain contents of the file that I would see in any text editor, and not the original data. I have no idea what I'm doing wrong.

问题是当我读回文件并将我读到的内容写入屏幕时,它只是我在任何文本编辑器中看到的文件的简单内容,而不是原始数据。我不知道我做错了什么。

I'm pretty sure line 67 is at fault, something to do with the way I am printing it to the screen, but I'm very new to C and not used to juggling formats of data.

我很确定第67行有问题,这与我将它打印到屏幕上的方式有关,但我对C很新,并且不习惯于处理数据格式。

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

int main ()
{

    FILE * pdataFile = NULL;
    const char * datafilename = "data.bin";
    const char * _WRITE = "wb"; 
    const char * _READ  = "rb"; 
    uint8_t data[32]; 
    int idx; 
    unsigned long dataFileLen; 
    char dataBuffer;

    char test_data[] = "ahFlup1r2PWO1zySK9SBcPIQC5DcCw1mq7JrObea8lDWH&FcLbi7EzBu7ow56KbJ"; 
    char * pos = test_data;



    pdataFile = fopen(datafilename, _READ);
    if (pdataFile == NULL) {
        printf("No existing file named: %s .\n", datafilename);

        /******** Print Contents of data Array ********/
        printf("Random data: "); 

        for (idx = 0; idx < 32; ++idx) {
            sscanf(pos, "%2hhx", &data[idx]);
            pos += 2 * sizeof(char); 
            printf("%02x", data[idx]);  
        };

        printf("\n"); // new line


        /******** Save data to File ********/
        pdataFile = fopen(datafilename, _WRITE);
        if (pdataFile == NULL) {
            printf("Error opening file %s for writing. End Program\n", datafilename); 
        } else {      
            fwrite (test_data, sizeof(char), sizeof(test_data), pdataFile); 

            if (ferror (pdataFile))
                printf("Error writing file %s.", datafilename);     

            fclose (pdataFile);
        };

    } else {
        fseek(pdataFile, 0, SEEK_END);
        dataFileLen = ftell(pdataFile);
        fseek(pdataFile, 0, SEEK_SET);

        char *dataBuffer = malloc((dataFileLen+1)*sizeof(unsigned char));

        fread(dataBuffer, dataFileLen, 1, pdataFile);

        if (ferror (pdataFile))
            {
                printf("Error reading file contents: %s.", datafilename);

            }else{
            printf("data read from file: ");            
            for(idx = 0; idx<dataFileLen; ++idx){ 
                printf("%2hhx", ((char *)dataBuffer)[idx]);  
            };
        };

        printf("\n"); // new line

        fclose (pdataFile);

    };

    return 0;
}; 

UPDATE I'm expecting to see the following: On first execution Random data: ahFlup1r2PWO1zySK9SBcPIQC5DcCw1mq7JrObea8lDWH&FcLbi7EzBu7ow56KbJ

更新我期待看到以下内容:首次执行随机数据:ahFlup1r2PWO1zySK9SBcPIQC5DcCw1mq7JrObea8lDWH&FcLbi7EzBu7ow56KbJ

On second execution data read from file: ahFlup1r2PWO1zySK9SBcPIQC5DcCw1mq7JrObea8lDWH&FcLbi7EzBu7ow56KbJ

从文件读取的第二个执行数据:ahFlup1r2PWO1zySK9SBcPIQC5DcCw1mq7JrObea8lDWH&FcLbi7EzBu7ow56KbJ

Second Update Even with the answers posted here I am still getting: Seed read from file: 6168466c757031723250574f317a79534b39534263504951433544634377266d71374a724f626561286c4457482646634c626937457a4275376f7735264b624a00

第二次更新即使在这里发布的答案,我仍然得到:种子从文件中读取:6168466c757031723250574f317a79534b39534263504951433544634377266d71374a724f626561286c4457482646634c626937457a4275376f7735264b624a00

The program is compiled in GCC and should compile from the code here.

该程序在GCC中编译,应该从这里的代码编译。

1 个解决方案

#1


You are probably writing the contents of the wrong array to the file.

您可能正在将错误数组的内容写入文件。

Try writing data instead of test_data.

尝试编写数据而不是test_data。

You will also likely want leading zeros on the values you print when reading the file (just as you already have when you initially print the scanned data).

您还可能希望在读取文件时打印的值为前导零(就像您最初打印扫描数据时一样)。

#1


You are probably writing the contents of the wrong array to the file.

您可能正在将错误数组的内容写入文件。

Try writing data instead of test_data.

尝试编写数据而不是test_data。

You will also likely want leading zeros on the values you print when reading the file (just as you already have when you initially print the scanned data).

您还可能希望在读取文件时打印的值为前导零(就像您最初打印扫描数据时一样)。