Hi there, ...
i want to read in a binary data file and to cast each pair of 2 bytes to a
signed short value. i try to different ways, but none results in what i
suppose it should result.
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
int main(){
Â*Â*Â*Â*Â*Â*Â*Â *charÂ*file_nam e[13];
Â*Â*Â*Â*Â*Â*Â*Â *FILE*Â*fptr;
Â*Â*Â*Â*Â*Â*Â* *signedÂ*short *c;
Â*Â*Â*Â*Â*Â*Â*Â *
Â*Â*Â*Â*Â*Â*Â* *printf("Enter *aÂ*filename:Â* ");
Â*Â*Â*Â*Â*Â*Â*Â *gets(file_name );
Â*Â*Â*Â*Â*Â*Â*Â *
Â*Â*Â*Â*Â*Â*Â*Â *//Â*VariationÂ*A: Â*UsingÂ*theÂ*s tandartÂ*fopen, Â*fgetc,Â*fclos eÂ*functions
Â*Â*Â*Â*Â*Â*Â*Â *ifÂ*((fptrÂ*=Â *fopen(file_nam e,"rb"))Â*==Â*0 ){
Â*Â*Â*Â*Â*Â*Â* *Â*Â*Â*Â*Â*Â*Â* Â*printf("Sorry .Â*Can'tÂ*open *fileÂ*\"%s\"\n ",Â*file_na me);
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*exitÂ*(EXIT_F AILURE);
Â*Â*Â*Â*Â*Â*Â*Â *}
Â*Â*Â*Â*Â*Â*Â*Â *for(intÂ*i=0;i <11;i++){
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*cÂ*=Â*fgetc(f ptr);
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*coutÂ*<<Â*cÂ* <<Â*"\t";
Â*Â*Â*Â*Â*Â*Â*Â *}
Â*Â*Â*Â*Â*Â*Â*Â *coutÂ*<<Â*"\n" ;
Â*Â*Â*Â*Â*Â*Â*Â *fclose(fptr);
Â*Â*Â*Â*Â*Â*Â*Â *//Â*VariationÂ*B: Â*UsingÂ*theÂ*f streamÂ*class
Â*Â*Â*Â*Â*Â*Â*Â *charÂ*buffer[24];
Â*Â*Â*Â*Â*Â*Â*Â *ifstreamÂ*date i(file_name,Â*i os::inÂ*|Â*ios: :binary);Â*Â*Â* Â*Â*Â*Â*
Â*Â*Â*Â*Â*Â*Â*Â *if(!datei){
Â*Â*Â*Â*Â*Â*Â* *Â*Â*Â*Â*Â*Â*Â* Â*printf("Sorry .Â*Can'tÂ*open *fileÂ*\"%s\"\n ",Â*file_na me);
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*exitÂ*(EXIT_F AILURE);
Â*Â*Â*Â*Â*Â*Â*Â *}
Â*Â*Â*Â*Â*Â*Â*Â *datei.read(buf fer,Â*24);
Â*Â*Â*Â*Â*Â*Â*Â *forÂ*(intÂ*i=0 ;i<24;i++){
Â*Â*Â*Â*Â*Â*Â*Â *Â*Â*Â*Â*Â*Â*Â* Â*coutÂ*<<Â*(si gnedÂ*short)buf fer[i]Â*Â*<<Â*"\t";
Â*Â*Â*Â*Â*Â*Â*Â *}
Â*Â*Â*Â*Â*Â*Â*Â *coutÂ*<<Â*"\n" ;
Â*Â*Â*Â*Â*Â*Â*Â *returnÂ*EXIT_S UCCESS;
}
You can test the program with the following data file:
Â*Â*Â*Â*Â*Â*Â*Â *http://www.uni-koblenz.de/~bastard/dat.1
The result that i suppose my program to print out can be generated with the
following perl-script:
Â*Â*Â*Â*Â*Â*Â*Â *http://www.uni-koblenz.de/~bastard/binaryDataRead
=> ./binaryDataRead dat.1
=> Result :
0Â*Â*Â*Â*Â*Â*Â*-489Â*Â*Â*Â*-458Â*Â*Â*Â*31Â* Â*Â*Â*Â*Â*474Â* Â*Â*Â*Â*-260Â*Â*Â*Â*-214Â*Â*Â*Â*-88Â*Â*Â*Â*Â*-241Â*Â*Â*Â*-112Â*Â*Â*
212Â*Â*Â*Â*Â*39 3Â*Â*Â*Â*Â*390
whereas the "0" is just an index of the printed record.
My Program results in:
=> Variation A:
23Â*Â*Â*Â*Â*Â*2 54Â*Â*Â*Â*Â*54Â *Â*Â*Â*Â*Â*254Â *Â*Â*Â*Â*31Â*Â* Â*Â*Â*Â*0Â*Â*Â* Â*Â*Â*Â*218Â*Â* Â*Â*Â*1Â*Â*Â*Â* Â*Â*Â*252Â*Â*Â* Â*Â*254Â*Â*Â*Â*
42Â*Â*Â*Â*Â*Â*2 55
=> Variation B:
23Â*Â*Â*Â*Â*Â*-2Â*Â*Â*Â*Â*Â*54 Â*Â*Â*Â*Â*Â*-2Â*Â*Â*Â*Â*Â*31 Â*Â*Â*Â*Â*Â*0Â* Â*Â*Â*Â*Â*Â*-38Â*Â*Â*Â*Â*1Â* Â*Â*Â*Â*Â*Â*-4Â*Â*Â*Â*Â*Â*-2Â*Â*Â*Â*Â*
42Â*Â*Â*Â*Â*Â*-1Â*Â*Â*Â*Â*Â*-88Â*Â*Â*Â*Â*-1Â*Â*Â*Â*Â*Â*15 Â*Â*Â*Â*Â*Â*-1Â*-112Â*Â*Â*Â*Â*-1Â*Â*Â*Â*Â*Â*-44Â*Â*Â*Â*Â*0Â* Â*Â*Â*Â*Â*
-119Â*Â*Â*Â*1Â*Â *Â*Â*Â*Â*Â*-122Â*Â*Â*Â*1
About Variation A:
i completely misunderstand the causal dependencies between the code and the
result.
About Variation B:
i see that i always cast and print out just 1 Byte. How can i cast and print
out 2 Bytes ? Are my approaches completly wrong ?
i hope anybody can help...
bye
Dimi