2014-08-27 6 views
0

I am beginner of C language.C- Is it right to read binary txt file?

I am trying to read a binary txt file.

When I just opened the txt file directly, I saw it

  • 젂 8 F ? ㎲?잤Oタsp쑴톧숴沛=)麴퓠5?r!오?쏠AD풠-V욕?C珞?|붆拂쟬v????걀신[email protected]풶1c퓋?>p璉>5쾳엥V壤?뾅A癬A?Αw 殆퀃[??픫퇡원?퓅V맙°쐴??閣?W條>걬벩3쁂횖?l?젖?옜鸚?d乍?涌h??伽?Y뤍XK?묠?RT?테[email protected]숦 ?vY햙y?kz'?n?6註웍Qt??*풯?인$???쾹y옇왯?L쁂碻?祠녋Dm+작\藪왱여?픻9c욋??출풛 궭母?좣?孔8퓎뒜C=瑾A??+샓?쇏\꾟퐾岫?j?s蕣풖M픑?앝I폹?=?f쏜 쌒뎠??빦갈i?_$?掠?;????2 & 풰Е?U윰i녉U?푂?풧+쁂-暉A倫겵헳퐉lg퓾닆욋ⓨ풇짡?믳빢쨒榕:@?뺠?쏱요p殆쩋쁂M됆A??욢??돽p瑥푢

Yes, the things in the txt file seem to be weird because it presents binary things.

The file consists of header and data.

The Header are

nSamples - number of samples in file (4-byte integer)

sampPeriod - sample period in 100ns units (4-byte integer)

sampSize - number of bytes per sample (2-byte integer)

parmKind - a code indicating the sample kind (2-byte integer)

and 4byte float type of data part.

My code is really doing well about the Header part.

but I don't know why it is going worse in the data part.

The data part seems to be wrong in my code because they have same values except data[0]

Actually, they all have different values.

#include <stdio.h> 
#include <stdlib.h> // exit() 



    int main(void) 
    { 

    FILE *a; 



    int abc=1; 
    int ch; 
    int size; 
    int k; 

    float* data; 
    long* nSamples; 
    long* sampPeriod; 
    short* sampSize; 
    short* sampKind; 

    nSamples=(long*) malloc(sizeof(long)); 
    sampPeriod=(long*) malloc(sizeof(long)); 
    sampSize=(short*) malloc(sizeof(short)); 
    sampKind=(short*) malloc(sizeof(short)); 
    data=(float*) malloc(sizeof(float)*10); 
    //I Just wanted to check a few values 

    if ((a = fopen("noalter.txt", "rb")) == NULL) { 
     fputs("error!", stderr); 
     exit(1); 
    } 

    fread(nSamples,sizeof(long),1,a); 
    fread(sampPeriod,sizeof(long),1,a); 
    fread(sampSize,sizeof(short),1,a); 
    fread(sampKind,sizeof(short),1,a); 

    fread(data,sizeof(float),1,a); 
    fread(data,sizeof(float),1,a); 
    fread(data,sizeof(float),1,a); 

    printf("%ld \n",nSamples[0]); 
    printf("%ld \n",sampPeriod[0]); 
    printf("%d \n", sampSize[0]); 
    printf("%d \n", sampKind[0]); 
    printf("%f \n", data[0]); 
    printf("%f \n", data[1]); 
    printf("%f \n", data[2]); //I Just wanted to check a few values 


    fclose(a); 
    free(nSamples); 
    free(sampPeriod); 
    free(sampSize); 
    free(sampKind); 

    return 0; 
    } 
+4

For a start get rid of your mallocs and frees and just pass the address of the vars to fread. You don't need the added problems for your example. You can always convert to dynamic memory allocation when you know your stuff is working. – Joe

+0

ya. In which format you are writing you need to read in the same format! – Sathish

+3

What is a "binary txt file"? –

답변

3

When reading the data, you read into the same position of data all three times. You should do e.g.

fread(&data[0], sizeof(data[0]), 1, a); 
fread(&data[1], sizeof(data[1]), 1, a); 
fread(&data[2], sizeof(data[2]), 1, a); 

Or just read all ten entries you have allocated:

fread(data, sizeof(data[0]), 10, a); 
+0

믿을 수 없다 !! 정말 고맙습니다. 매우 화려하다!! – user2874612

0

If you are using Linux the contents of the binary file can also be used using cat command.

+0

조언 해 주셔서 감사합니다. 내가 리눅스에서 이것을 시도 할 때, 나는 그것을 기억한다! – user2874612