Reading Binary files

This is a discussion on Reading Binary files within the C++ Programming forums, part of the General Programming Boards category; Hey, I've looked over the binary I/O post and read over some tutorials on line, but I still can't make ...

  1. #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?

  2. #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*.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Nope, doesn't work either.
    Thanks though.

  4. #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

  5. #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 ''

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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?

  7. #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);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  8. #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

  9. #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);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  11. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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