Thread: Syntax for file.read() using std::bitset??

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Syntax for file.read() using std::bitset??

    Hi all,
    I'm trying to read-in some binary data from a file, into a bitset. I was doing this:

    Code:
    bitset<FLAG_SIZE>  flags;
    file.read((char *) &flags, FLAG_SIZE);
    but found my stack was getting corrupted when I was doing the read(). It seemed to work most of the time, but not all of the time.

    I've tried other syntax with no luck. Any ideas?? Can I read binary data from a file directly into a bitset? (STL newbie)

    Thanks for any pointers.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    On my machine, a bitset<N> takes up 4 bytes for 1 < N <= 32 and 8 bytes for 32 < N <= 64 so you can see that it grows by basically the size of my machines basic int data type. If FLAG_SIZE happens to 16 for instance, then you are trying to read 16 bytes worth of data and stuff it into a 4 byte area of memory. What do you think is going to happen here?

    Code:
    bitset<32> bits32;
    bitset<33> bits33;
    bitset<64> bits64;
    bitset<65> bits65;
    cout << sizeof(bits32) << endl;  // outputs 4
    cout << sizeof(bits33) << endl;  // outputs 8
    cout << sizeof(bits64) << endl;  // outputs 8
    cout << sizeof(bits65) << endl;  // outputs 12
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    NEVER try to directly read/write non-POD classes!

    bitset has methods for conversion from/to string and long, use these.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    41
    hk_mp5kpw> Thanks!! That fixed it!

    CornedBee> Thanks for the tip. I tried different syntax for the to_string method. The book I'm using suggests

    flags.template to_string<char>()

    however /usr/include/c++/3.2.2/bitset
    gives the example of

    flags.to_string<char,char_traits<char>,allocator<c har> >()

    and neither of them work ("no matching function....."), so I went with the (char *). I'll keep muddling with the syntax though, thanks.


  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Something like this might work.
    Code:
    char buffer[FLAG_SIZE];
    file.read((char *) &buffer, FLAG_SIZE);
    string input(buffer,FLAG_SIZE); // Convert char array into string
    bitset<FLAG_SIZE>  flags(input);  // Convert string into bitset
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Actually bitset overloads the stream operators.
    Code:
    bitset<27> bs; // Fill it
    ofstream ofs("bs.dat");
    ofs << bs << endl;
    ofs.close();
    ifstream ifs("bs.dat");
    ifs >> bs;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM