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_name[13];
Â*Â*Â*Â*Â*Â*Â*Â*FILE*Â*fptr;
Â*Â*Â*Â*Â*Â*Â*Â*signedÂ*shortÂ*c;
Â*Â*Â*Â*Â*Â*Â*Â*
Â*Â*Â*Â*Â*Â*Â*Â*printf("EnterÂ*aÂ*filename:Â*");
Â*Â*Â*Â*Â*Â*Â*Â*gets(file_name);
Â*Â*Â*Â*Â*Â*Â*Â*
Â*Â*Â*Â*Â*Â*Â*Â*//Â*VariationÂ*A:Â*UsingÂ*theÂ*standartÂ*fopen,Â*fge tc,Â*fcloseÂ*functions
Â*Â*Â*Â*Â*Â*Â*Â*ifÂ*((fptrÂ*=Â*fopen(file_name,"rb "))Â*==Â*0){
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*printf("Sorry.Â*Ca n'tÂ*openÂ*fileÂ*\"%s\"\n",Â*file_name);
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*exitÂ*(EXIT_FAILUR E);
Â*Â*Â*Â*Â*Â*Â*Â*}
Â*Â*Â*Â*Â*Â*Â*Â*for(intÂ*i=0;i<11;i++){
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*cÂ*=Â*fgetc(fptr);
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*coutÂ*<<Â*cÂ*<<Â*" \t";
Â*Â*Â*Â*Â*Â*Â*Â*}
Â*Â*Â*Â*Â*Â*Â*Â*coutÂ*<<Â*"\n";
Â*Â*Â*Â*Â*Â*Â*Â*fclose(fptr);
Â*Â*Â*Â*Â*Â*Â*Â*//Â*VariationÂ*B:Â*UsingÂ*theÂ*fstreamÂ*class
Â*Â*Â*Â*Â*Â*Â*Â*charÂ*buffer[24];
Â*Â*Â*Â*Â*Â*Â*Â*ifstreamÂ*datei(file_name,Â*ios::i nÂ*|Â*ios::binary);Â*Â*Â*Â*Â*Â*Â*
Â*Â*Â*Â*Â*Â*Â*Â*if(!datei){
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*printf("Sorry.Â*Ca n'tÂ*openÂ*fileÂ*\"%s\"\n",Â*file_name);
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*exitÂ*(EXIT_FAILUR E);
Â*Â*Â*Â*Â*Â*Â*Â*}
Â*Â*Â*Â*Â*Â*Â*Â*datei.read(buffer,Â*24);
Â*Â*Â*Â*Â*Â*Â*Â*forÂ*(intÂ*i=0;i<24;i++){
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*coutÂ*<<Â*(signed *short)buffer[i]Â*Â*<<Â*"\t";
Â*Â*Â*Â*Â*Â*Â*Â*}
Â*Â*Â*Â*Â*Â*Â*Â*coutÂ*<<Â*"\n";
Â*Â*Â*Â*Â*Â*Â*Â*returnÂ*EXIT_SUCCESS;
}
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Â*Â*Â*Â*Â*393Â*Â*Â*Â*Â*390
whereas the "0" is just an index of the printed record.
My Program results in:
=> Variation A:
23Â*Â*Â*Â*Â*Â*254Â*Â*Â*Â*Â*54Â*Â*Â*Â*Â*Â*254Â*Â*Â* Â*Â*31Â*Â*Â*Â*Â*Â*0Â*Â*Â*Â*Â*Â*Â*218Â*Â*Â*Â*Â*1Â*Â *Â*Â*Â*Â*Â*252Â*Â*Â*Â*Â*254Â*Â*Â*Â*
42Â*Â*Â*Â*Â*Â*255
=> 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