fstream messes up order at read out

  • Thread starter Christian Henke
  • Start date
C

Christian Henke

Hi!

maybe someone can help me?!
I want to use fstream to read an .au file . This .au file includes
multiple information in its header. a MagicNumber, the No of Channels
used, and so on.
In hex code the data is 2e736e64 (the magic number). But when i read
the number with fstream i get 646e732e. Why are the numbers messed up
in order?!
I am using g++ 2.95.

Here is my source code:

AudioFile::AudioFile(char* auFile_Name)
{ // konstruktor übergebende Name ist die einzulesende Datei
Filename = auFile_Name;
fstream ein(auFile_Name, ios::in || ios::binary);
ein.read((char *) &(AuHeader), sizeof(struct Header_Data));
// struct Header_Data is shown below in .h file
cout << AuHeader.Magic_Number;
cout << AuHeader.DataLength;
}

typedef struct Header_Data { // Attributreihe die im Header der
..au Dateien enthalten ist
signed int Magic_Number;
signed int Header_Size;
signed int Data_Length;
signed int Encoding_Type;
signed int Sample_Rate; // Abtastfrequenz
signed int Channels; // Mono oder Stereo
} Header_Data;

Thanks

Chris
 
J

John Harrison

Christian Henke said:
Hi!

maybe someone can help me?!
I want to use fstream to read an .au file . This .au file includes
multiple information in its header. a MagicNumber, the No of Channels
used, and so on.
In hex code the data is 2e736e64 (the magic number). But when i read
the number with fstream i get 646e732e. Why are the numbers messed up
in order?!

Because different computer write binary numbers in different ways. For the
number 2e736e64, some computer will write this out as

2e 73 6e 64

and some will write it out as

64 6e 73 2e

Thereoretically at least other possibilities exist to.

The lesson is that you cannot expect to write a number in binary on one
computer and read it in on another computer and expect the numbers to be the
same.

In your case you have to manipulate the number by swapping round the bytes
so the number is what you expect.

john
 
D

David Harmon

On 6 Mar 2004 06:18:25 -0800 in comp.lang.c++, (e-mail address removed)
(Christian Henke) wrote,
fstream ein(auFile_Name, ios::in || ios::binary);

#include <iso646.h>
fstream ein(auFile_Name, ios::in bitor ios::binary);
 
R

Rob Williscroft

David Harmon wrote in
On 6 Mar 2004 06:18:25 -0800 in comp.lang.c++, (e-mail address removed)
(Christian Henke) wrote,

This is standard C *not* C++:
#include <iso646.h>

This is Standard C++ (without the above include):

#include <fstream>
#include <ios>

using namespace std;
fstream ein(auFile_Name, ios::in bitor ios::binary);

A complete programme (called 'test.cpp'):

#include <iostream>
#include <fstream>
#include <ios>

using namespace std;

fstream file( "test.cpp", ios::in bitor ios::binary );

int main()
{
unsigned a = 0x1, b = 0x2;

cerr << ( a bitor b ) << '\n';
cerr << file.rdbuf();
}

I still prefer:

fstream ein(auFile_Name, ios::in | ios::binary);

Though :).

Rob.
 
C

Christian Henke

Thanks for your help John and David.

@john: When i open the .au file with ghex then the numbers are in the
correct order, so i think the order is not depended on the computer im
using.

@david: I'm not sure why including <iso646.h> should help.

I'm having even more problems now. The snippet won't even work on the
pc from my project group. The are using g++ 3.1.3 while I'm using
2.8..
3.1.3 does not even know fstream. I can't find any other headerfile.
the book I'm working with by Gerhard Willms is propably to old (2001).
 
D

David Harmon

On 6 Mar 2004 15:44:05 -0800 in comp.lang.c++, (e-mail address removed)
(Christian Henke) wrote,
@david: I'm not sure why including <iso646.h> should help.

Nobody appreciates the humor of iso646.h.

The include is probably required if you use the bitor keyword instead of
the | operator. I claim that bitor avoids the risk of typo'ing ||.
Either way, || is wrong for ios bit flags. It's not your current
trouble, but is bound to bite you eventually if not fixed.
 
J

John Harrison

Christian Henke said:
Thanks for your help John and David.

@john: When i open the .au file with ghex then the numbers are in the
correct order, so i think the order is not depended on the computer im
using.

What is ghex?

It's a fact that order of bytes in a binary number is platform dependent.
Since reading with fstream gives you the bytes in the wrong order I think
you must have them in the wrong order for your platform. Maybe ghex swaps
the bytes around for you.

If you want to research this for yourself, the order of bytes in a binary
number is called it's 'endian-ness'. Maybe you could look up 'endian' on the
internet.
@david: I'm not sure why including <iso646.h> should help.

I'm having even more problems now. The snippet won't even work on the
pc from my project group. The are using g++ 3.1.3 while I'm using
2.8..
3.1.3 does not even know fstream. I can't find any other headerfile.
the book I'm working with by Gerhard Willms is propably to old (2001).

Of course 3.1.3 knows about fstream, its perfectly standard C++. The correct
header file is <fstream> (without .h). Maybe you don't know about
namespaces?

#include <fstream>
using namespace std;

fstream myFile;

john
 
C

Christian Henke

@john:

Thanks for your advice. We used namespace at our course but i didn't
know what it is supposed to do. Now ik know. Unfortunately the linker
of .o files now throws error messages. It seems to me that methods
from fstream like terminate have to be implemented, but i'm not sure.
The error messages:
undefinde reference to .. throw undefined reference to .. terminate
....

I will read a couple chapters in my book maybe this will help.
And i will look up endian on the net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top