473,399 Members | 3,401 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,399 software developers and data experts.

fstream messes up order at read out

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
Jul 22 '05 #1
7 1765

"Christian Henke" <sc*********@gmx.de> wrote in message
news:94**************************@posting.google.c om...
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
Jul 22 '05 #2
On 6 Mar 2004 06:18:25 -0800 in comp.lang.c++, sc*********@gmx.de
(Christian Henke) wrote,
fstream ein(auFile_Name, ios::in || ios::binary);


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

Jul 22 '05 #3
David Harmon wrote in news:40***************@news.west.earthlink.net:
On 6 Mar 2004 06:18:25 -0800 in comp.lang.c++, sc*********@gmx.de
(Christian Henke) wrote,
fstream ein(auFile_Name, ios::in || ios::binary);


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
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).
Jul 22 '05 #5
On 6 Mar 2004 15:44:05 -0800 in comp.lang.c++, sc*********@gmx.de
(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.

Jul 22 '05 #6

"Christian Henke" <sc*********@gmx.de> wrote in message
news:94**************************@posting.google.c om...
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
Jul 22 '05 #7
@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.
Jul 22 '05 #8

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

Similar topics

2
by: Maya | last post by:
Apart from being a pointer, what would be the benefit of using 'std::filebuf' than using the std::fstream? As far as I can see, I would use the same methods in 'filebuf' that I would when using...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
3
by: kieran | last post by:
Hi, I'm using fstream.get to read in a character from a file, then print it on the screen. I have a file called test.log that contains "Hello, World!", but when I try and print the contents out on...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
6
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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...
0
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
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...

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.