473,545 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_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
Jul 23 '05 #1
4 1294
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(buff er, 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(buff er, 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(buff er, 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
2809
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 to the printer to standard in. You have to provide yourself the program that does something with that data, so I thought with some quick c/c++...
50
4887
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
1
2531
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 struct, in this file there are lot of records i need to loop trough, first time I read the file, the data is fine but for the rest the data is not...
5
6433
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 internal override string ReadString() { stringLength = fileStream.ReadByte(); moInput.Read(byteArrayBuffer, 0, stringLength); return...
7
6044
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an...
2
3257
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs...
9
2186
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 C and some, I must admit, have not really sunk in yet. Still, I push on. Now I am taken a library of C programs that were designed to read HDF...
8
1517
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 the format is arranged primarily in single-byte unsigned integers. So i've been reading them in a chars and casting them as unsigned chars into an...
2
5830
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 point it stops and wont read any more values. I use this code to open it and read it: if( (err = fopen_s( &fp,"C:/simfile1.tmg0" , "rb" )) != 0...
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5341
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.