【问题标题】:C - binary Char to doubleC - 二进制字符加倍
【发布时间】:2020-05-20 09:23:01
【问题描述】:

我们接到了大学的编程任务。我们应该编写一个从 bin 文件中读取双精度值的程序。 bin 文件中的 double 值具有如下外观:

程序成功将所有值读取为CharArray,但是我不明白如何将这个带有值的Char数组转换为double。 我还没有编程 C 太久。我希望有人可以帮助我找到解决方案。

到目前为止我的代码:

typedef struct {
    char magic[16];
    char dataType[16];
    char elementCount[16];
} FileHeader;

typedef struct {
    unsigned int n;
    double *values;
} FileData;

//---METHODEN 

FileData readBinaryValues (const char *filename){

    FileData error;
    error.n = 0;
    error.values = NULL;

    FILE *datei = fopen(filename, "rb"); 
    if (datei == NULL){
        fprintf(stderr,"ERROR: Datei nicht gefunden\n");
        return error;
    }

    FileHeader fH;
    fscanf(datei,"%16c%16c%16c",fH.magic,fH.dataType,fH.elementCount );

    char testMagic[16] = "STATDATA";
    char testDataType[16] = "DOUBLE";
    if ( strcmp(testMagic,fH.magic) != 0){

        fprintf(stderr,"ERROR: Unerwarteter Magic\n");
        printf("%s\n",fH.magic);
        fclose(datei);
        printf("%s\n",fH.magic);
        return error;

    } else if (strcmp(testDataType,fH.dataType) != 0){

        fprintf(stderr,"ERROR: Unerwarteter DataType\n");
        fclose(datei);
        return error;

    } else {

        int elmentCount = atoi(fH.elementCount);
        FileData result;
        result.values = (double*) malloc(elmentCount * sizeof(double));

        if (result.values == NULL){

            fprintf(stderr,"ERROR: Zu wenig HeapSpeicher\n");
            fclose(datei);
            return error;
        }

        int posE = (3*16)-1; 
        fseek(datei, posE , SEEK_SET); //start double-elements 




        char memBlock [sizeof(double)]; //memoryblock 


        int j = 0;
        int i  = 0;
        for (j=0;j<=10;j++){ //only 10 doubles for testing 

            for (i = 0;i<(int)sizeof(double);i++){ //sizeof(double) durchläufe 


                memBlock[i] = fgetc(datei);
            }

            if(memBlock == NULL){ //fängt nichts ab 
                fprintf(stderr,"ERROR: Unerwartetes Dateiende\n");
                fclose(datei);
                return error;
            }

            printf("%x\n",memBlock);
            snprintf(memBlock,sizeof(double),"%.8e", result.values[j]);

            //result.values[j] =strtod(memBlock,NULL);
            //result.values (double*)memBlock;
            //memcpy(&result.values[j],memBlock,sizeof(double));
            //result.values[j] = atof(memBlock);
            result.values[j] = *((double*)memBlock);
            //result.values[j] = atof(memBlock); //-------------------- string in double 
            //memcpy(&result.values[j],memBlock,sizeof(double)); //bin -> double

            printf("%i)%lf\n",j,result.values[j]);
        }
    result.n = elmentCount; 
    return result;
    }


}

抱歉,但有些 cmets 和错误是用德语写的。 FileHeader 描述了 .bin 文件的外观。 Magic 首先作为 dataType 出现,然后是 .bin 中的 double 元素的数量。紧随其后的是这么多双值。 程序仍然检查magic = STATDATA 和dataType = DOUBLE。

【问题讨论】:

  • IEEE 754 双精度浮点值占用 8 字节空间。为什么你的数组有 16 个字节长?无论如何,如果数据存储为 IEEE 754,并且文件中的字节序与您机器的字节序匹配,那么您可以简单地 copy the array into a double using memcpy。如果您需要将大端转换为小端或反之,则必须先反转数组。

标签: c char double bin


【解决方案1】:

查看

strtod()

文档here

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 2017-06-05
    • 1970-01-01
    相关资源
    最近更新 更多