D
Dimitri Papoutsis
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, fgetc, fclose functions
        if ((fptr = fopen(file_name,"rb")) == 0){
                printf("Sorry. Can't open file \"%s\"\n", file_name);
                exit (EXIT_FAILURE);
        }
        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::in | ios::binary);      Â
        if(!datei){
                printf("Sorry. Can't open file \"%s\"\n", file_name);
                exit (EXIT_FAILURE);
        }
        datei.read(buffer, 24);
        for (int i=0;i<24;i++){
                cout << (signed short)buffer  << "\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
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, fgetc, fclose functions
        if ((fptr = fopen(file_name,"rb")) == 0){
                printf("Sorry. Can't open file \"%s\"\n", file_name);
                exit (EXIT_FAILURE);
        }
        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::in | ios::binary);      Â
        if(!datei){
                printf("Sorry. Can't open file \"%s\"\n", file_name);
                exit (EXIT_FAILURE);
        }
        datei.read(buffer, 24);
        for (int i=0;i<24;i++){
                cout << (signed short)buffer  << "\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