C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2005, 10:01 AM   #1
Registered User
 
Join Date: May 2005
Location: Toronto, Canada
Posts: 257
Reading Binary files

Hey,

I've looked over the binary I/O post and read over some tutorials on line, but I still can't make the program read in a binary number properly.

I've tried:
Code:
 char num[5];
   int DataFormat;

   inp>>DataFormat;

   // and 

   inp.read(num, sizeof(long)); 
   num[5] = '\0';
   DataFormat = atoi(num);
It doesn't interprit DataFormat properly in either case. The input file is binary. to place the poiter at the correct position I use:
Code:
ifstream inp;
   inp.open("test.baf", ios_base::binary);

    inp.seekg(549,ios::beg);
is there another way to read in 4 bytes into a longint variable fro ma binary file?
earth_angel is offline   Reply With Quote
Old 07-11-2005, 10:10 AM   #2
Registered User
 
Join Date: Jul 2005
Posts: 25
Try
Code:
long x;

inp.read(&x, sizeof(long);
You might need to cast &x to char*.
manannan is offline   Reply With Quote
Old 07-11-2005, 11:06 AM   #3
Registered User
 
Join Date: May 2005
Location: Toronto, Canada
Posts: 257
Nope, doesn't work either.
Thanks though.
earth_angel is offline   Reply With Quote
Old 07-11-2005, 11:28 AM   #4
Registered User
 
Join Date: Nov 2001
Posts: 1,348
Check out this line:

num[5] = '\0';

Post an example of the bytes that is read from the file and the incorrect value assigned to DataFormat.

Kuphryn
kuphryn is offline   Reply With Quote
Old 07-11-2005, 11:55 AM   #5
Registered User
 
Join Date: May 2005
Location: Toronto, Canada
Posts: 257
Code:
	DataFormat	-858993460
	&DataFormat	0x0012f5bc
thant's using : inp.read((char*)&DataFormat, sizeof(long));

If I use
Code:
inp.read(num, sizeof(long)); 
   num[5] = '\0';
   DataFormat = atoi(num);
I get
Code:
	DataFormat	-858993460
-	num	0x0012f5c0 "ÌÌÌÌÌ"
	[0]	-52 'Ì'
	[1]	-52 'Ì'
	[2]	-52 'Ì'
	[3]	-52 'Ì'
	[4]	-52 'Ì'
	num[5]	0 ''
earth_angel is offline   Reply With Quote
Old 07-11-2005, 12:17 PM   #6
Registered User
 
Join Date: Oct 2001
Posts: 2,936
Code:
inp.read((char*)&DataFormat, sizeof(long));
is correct (assuming a 4 byte int). Or better is:
Code:
inp.read((char*)&DataFormat, sizeof(DataFormat));
What's actually in your file at locations 549-552?
__________________
http://www.freechess.org
swoopy is offline   Reply With Quote
Old 07-11-2005, 12:27 PM   #7
The N00b That Owns You!
 
Join Date: Jul 2005
Location: Canada!
Posts: 178
this is advance but i remember that you have to treat the file as binary while opening it

Code:
ofstream file ( "file.file", ios::binary );
__________________
New Function!!!!

glAddIdol(C+noob);

Quote:
The feeling of rusty spoons against my salad fingers is almost ORGASMIC
C+noob is offline   Reply With Quote
Old 07-11-2005, 12:32 PM   #8
Registered User
 
Join Date: May 2005
Location: Toronto, Canada
Posts: 257
>What's actually in your file at locations 549-552?

It should be 0,1 or 2 as a long int.

>you have to treat the file as binary while opening it
I do use ios::binary
earth_angel is offline   Reply With Quote
Old 07-11-2005, 12:43 PM   #9
The N00b That Owns You!
 
Join Date: Jul 2005
Location: Canada!
Posts: 178
god im stupid lol didnt read all of it hahaha sry
__________________
New Function!!!!

glAddIdol(C+noob);

Quote:
The feeling of rusty spoons against my salad fingers is almost ORGASMIC
C+noob is offline   Reply With Quote
Old 07-11-2005, 04:39 PM   #10
Registered User
 
Join Date: Oct 2001
Posts: 2,936
I would verify the data with this then (first seek to appropriate location):
Code:
char ch;

inp.read((char*)&ch, sizeof(ch));
cout << (int) ch << " ";
inp.read((char*)&ch, sizeof(ch));
cout << (int) ch << " ";
inp.read((char*)&ch, sizeof(ch));
cout << (int) ch << " ";
inp.read((char*)&ch, sizeof(ch));
cout << (int) ch << " ";
cout << endl;
It's possible the bytes are reversed (endian problem), but first see if the data looks right.
__________________
http://www.freechess.org
swoopy is offline   Reply With Quote
Old 07-12-2005, 06:48 AM   #11
Registered User
 
Join Date: May 2005
Location: Toronto, Canada
Posts: 257
I've tried everything above. It doesn't come back properly. let me look into the file structure a bit more. Maybe I'm forgetting something there. Maybe later I'll be back with a new problem.
Thanks for all the help,

AS.
earth_angel is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file in binary question Dan17 C++ Programming 3 05-17-2006 12:28 AM
Binary files Lionmane C Programming 35 08-25-2005 01:56 PM
Reading data from a binary file John22 C Programming 7 12-06-2002 02:00 PM
problem reading files in C angelfly C Programming 9 10-10-2001 11:58 AM
Need Advice in reading files jon C Programming 4 10-07-2001 07:27 AM


All times are GMT -6. The time now is 11:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22