473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problems with reading binary data files

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
Jul 23 '05 #1
4 1286
Dimitri Papoutsis wrote:
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.
datei.read(buffer, 24);
for (int i=0;i<24;i++){
cout << (signed short)buffer[i] << "\t";
}


buffer[i] reads a char prior to the cast. To read 2 bytes you can
dereference a pointer to a 2 byte type.

signed short* p = (signed short*) &buffer[i];
signed short result = *p;

You will also need to modify your loop so i increments 0, 2, 4, ...

--
Scott McPhillips [VC++ MVP]

Jul 23 '05 #2
Scott McPhillips [MVP] wrote:
Dimitri Papoutsis wrote:
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.
datei.read(buffer, 24);
for (int i=0;i<24;i++){
cout << (signed short)buffer[i] << "\t";
}

buffer[i] reads a char prior to the cast. To read 2 bytes you can
dereference a pointer to a 2 byte type.

signed short* p = (signed short*) &buffer[i];
signed short result = *p;

You will also need to modify your loop so i increments 0, 2, 4, ...


A better method for reading multibyte binary integers is to
read each byte individually and build the value. This will
allow the proper reading of the data file regardless of the
Endianism of the platform.

--
Thomas
Jul 23 '05 #3
Dimitri Papoutsis wrote:
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> non standard header. Use <iostream>
#include <fstream.h> non standard header. Use <fstream>


int main(){

[redacted]
Jul 23 '05 #4
Scott McPhillips [MVP] wrote:
Dimitri Papoutsis wrote:
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.
datei.read(buffer, 24);
for (int i=0;i<24;i++){
cout << (signed short)buffer[i] << "\t";
}
buffer[i] reads a char prior to the cast. To read 2 bytes you can
dereference a pointer to a 2 byte type.

signed short* p = (signed short*) &buffer[i];


Undefined behaviour, if buffer + i isn't correctly aligned
for a short. Also, 'short' is signed, you don't have to write
out the word signed.

Better would be:

short result;
memcpy(&result, buffer, sizeof result);

although this may not work on esoteric systems.

You will also need to modify your loop so i increments 0, 2, 4, ...

--
Scott McPhillips [VC++ MVP]


Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been able to read data from binary files to filestrean, the data in the files are structured, what I'm trying to do is to loop through the binary files and add data to my...
5
by: rnorthedge | last post by:
I am working on a code library which needs to read in the data from large binary files. The files hold int, double and string data. This is the code for reading in the strings: protected...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
9
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about...
8
by: psy_berpunk | last post by:
hey, i'm trying to write a simple program to read gif87a non- interlaced format with a single image-descriptor --- I am using djgpp on windows xp. Sounds simple enough, unfortunatly data in...
2
by: steve005 | last post by:
Hi, I am writing a binary file with matlab that consists of a couple hundred double values and then reading it in with c++. The problem arises in c++ when I try and read in the files. At a specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.