![]() |
| | #1 |
| Registered User Join Date: May 2005 Location: Toronto, Canada
Posts: 257
| Reading Binary files 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); Code: ifstream inp;
inp.open("test.baf", ios_base::binary);
inp.seekg(549,ios::beg);
|
| earth_angel is offline | |
| | #2 |
| Registered User Join Date: Jul 2005
Posts: 25
| Try Code: long x; inp.read(&x, sizeof(long); |
| manannan is offline | |
| | #3 |
| Registered User Join Date: May 2005 Location: Toronto, Canada
Posts: 257
| Nope, doesn't work either. Thanks though. |
| earth_angel is offline | |
| | #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 | |
| | #5 |
| Registered User Join Date: May 2005 Location: Toronto, Canada
Posts: 257
| Code: DataFormat -858993460 &DataFormat 0x0012f5bc If I use Code: inp.read(num, sizeof(long)); num[5] = '\0'; DataFormat = atoi(num); Code: DataFormat -858993460 - num 0x0012f5c0 "ÌÌÌÌÌ" [0] -52 'Ì' [1] -52 'Ì' [2] -52 'Ì' [3] -52 'Ì' [4] -52 'Ì' num[5] 0 '' |
| earth_angel is offline | |
| | #6 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| Code: inp.read((char*)&DataFormat, sizeof(long)); Code: inp.read((char*)&DataFormat, sizeof(DataFormat));
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #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:
| |
| C+noob is offline | |
| | #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 | |
| | #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:
| |
| C+noob is offline | |
| | #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;
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |