473,493 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

binary data conversion

Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mystruct>
I'm having a helluva time with the binary-->(pod)datatype conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F0000140501000B0000000000
993A000066C70000140501000F0000000000
C0880000538D0100140501001D0000000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_offset; // 4 bytes
};

Is this one of those times fscanf IS the "better" (whatever that means) way?

Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

Thank you for your thoughts,
Drake
Nov 14 '05 #1
7 1102
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mystruct>


Bummer. Ask C++ questions in a C++ newsgroup like news:comp.lang.c++
Nov 14 '05 #2

"Drake" <x@y.net> wrote in message
news:Xn***********************@207.217.125.204...
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mystruct>
I'm having a helluva time with the binary-->(pod)datatype conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F0000140501000B0000000000
993A000066C70000140501000F0000000000
C0880000538D0100140501001D0000000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_offset; // 4 bytes
};
I'll assume those byte sizes in your comments refer
to your particular implementation. The language
doesn't require those sizes (but it does require
minimum sizes).

Is this one of those times fscanf IS the "better" (whatever that means) way?
Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?


fscanf() is not what you need. It reads textual data.

Use fread(). And be sure to account for 'endianness'.

-Mike
Nov 14 '05 #3
Mac
On Sun, 11 Apr 2004 00:14:44 +0000, Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mystruct>
I'm having a helluva time with the binary-->(pod)datatype conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F0000140501000B0000000000
993A000066C70000140501000F0000000000
C0880000538D0100140501001D0000000000
Is this a hex dump of the file, or is the file text... You said it was
binary earlier.

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_offset; // 4 bytes
};

Is this one of those times fscanf IS the "better" (whatever that means) way?

Well, if the file is text, I think fscanf() is probably the best way, but
you can do whatever you want, including reading a character at a time with
getc() and not calling any other library functions. Another function to
consider is fgets() (again, assuming a text file.)

If the file is binary, which is what you said initially, I definitely
would not use fscanf(). I would use getc() or fread().
Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

What do you mean by "powers?" Is that short for "powers that be," meaning,
loosely, whoever is in charge?
Thank you for your thoughts,
Drake

I think you may want comp.lang.c++.

If you want a c solution, I suggest you rephrase to make that more clear,
removing all reference to c++, just to be safe, and repost.

If you want a c++ solution, post in comp.lang.c++, but read their faq
and/or lurk first to make sure you don't annoy anyone.

--Mac
Nov 14 '05 #4
Martin Ambuhl <ma*****@earthlink.net> wrote in news:c5a47k$2m9rj0$3@ID-
227552.news.uni-berlin.de:
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mystruct>


Bummer. Ask C++ questions in a C++ newsgroup like news:comp.lang.c++

A few lines later the post tried and obviously failed in avoiding this
answer.

<< Although I've referred to C++ containers, iostreams
<< are not used... FILE* only. Insert/extract operators are not an
<< option.

Anyway std::vector isn't the problem. It's the conversion.
Drake
Nov 14 '05 #5
"Drake" <x@.y.net> wrote in message
news:Ju*****************@newsread2.news.pas.earthl ink.net...
Martin Ambuhl <ma*****@earthlink.net> wrote in news:c5a47k$2m9rj0$3@ID-
227552.news.uni-berlin.de:
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mystruct>


Bummer. Ask C++ questions in a C++ newsgroup like news:comp.lang.c++

A few lines later the post tried and obviously failed in avoiding this
answer.

<< Although I've referred to C++ containers, iostreams
<< are not used... FILE* only. Insert/extract operators are not an
<< option.

Anyway std::vector isn't the problem. It's the conversion.
Drake


Tastefully dispatched. ;-)

Nov 14 '05 #6

"Drake" <x@y.net> wrote in message

Here's 3 records (broken into lines for easy reading)...

D5840000428F0000140501000B0000000000
993A000066C70000140501000F0000000000
C0880000538D0100140501001D0000000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_offset; // 4 bytes
};

Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

As someone else has pointed out, you don't say whether your example is a
hexdump or a text file. fscanf() is useful only for text files, but of
limited utlity if numerics are not separated by delimiters.

What you need are the functions

unsigned long read32us(FILE *fp);
unsigned short read16us(FILE *fp);

If you want to use C++ you can be ugly and replace the FILE * with a stream,
or be object-oriented and derive a class from iostream with the functions
added.
In either case, build the read32() function on top of fgetc() / character
extraction.
If you are using C++, throw exceptions in the case of premature EOF.
Nov 14 '05 #7
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mystruct>
I'm having a helluva time with the binary-->(pod)datatype conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F0000140501000B0000000000
993A000066C70000140501000F0000000000
C0880000538D0100140501001D0000000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_offset; // 4 bytes
};

Is this one of those times fscanf IS the "better" (whatever that means) way?

Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

Thank you for your thoughts,
Drake


If your file is ASCII encoded hex file (a.k.a. Hex dump),
then you may want to read each line using fgets(), then
fill in your data structure by converting the ASCII values
into native formats. This is probably the faster method
than fscanf.

If the file is actually binary, you may want to use the
fread function and read a block into a buffer then load
the fields of your structure from the buffer.

In no case do you want to load your structure directly
from the file or memory. The simple rule is that the
size of a structure may not be equal to the size of its
members. The compiler is allowed to add "padding" bytes
between members. This is what kills most programs that
attempt to map a structure directly to an input stream.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #8

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

Similar topics

7
4200
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
103
48475
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
2
3609
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats;...
9
2266
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
26
4264
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
10
3825
by: Wrecked | last post by:
Could someone suggest a method to convert a Binary data to an ASCII data, with very less or no increase in the memory . The problem basically is there is an excrypted message which needs to be...
7
6976
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
1
2740
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef...
7
19191
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
12
3676
by: fermineutron | last post by:
I am trying to write a function which will convert a large ascii number, say 100 ascii digits, to its binary representation. It seems that evey algorithm I am trying to think of is backwards. ...
0
7119
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,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
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,...
0
7195
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...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5453
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,...
0
4579
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...
0
3088
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...
0
285
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...

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.