简体   繁体   中英

ANSI C What am I doing wrong with fwrite and the fread?

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.

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.

#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

On second execution data read from file: ahFlup1r2PWO1zySK9SBcPIQC5DcCw1mq7JrObea8lDWH&FcLbi7EzBu7ow56KbJ

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

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

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

Try writing data instead of 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM