Thread: writing to binary files....

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    writing to binary files....

    how do i write to a binary file without it thinking that I'm writing it as a normal file?

    ofstream savefile;

    savefile.open("DEFAULT.B3D",ios:ut | ios::binary | ios::trunc);

    for(int a=0;a<30;a++)
    {
    for(int b=0;b<30;b++)
    {
    savefile <<'a';
    }
    }
    savefile.close();

    that doesn't seem to work, i think it might be that I'm trying to use the <<, but is there another way to output to the file? (i'm trying to output a matrix to the file, but I used an a to see if it even worked......

    savefile << level[a] [b];

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    use the member fucntions read and write instead of operator>> and operator<<

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    savefile.write(level[a] [b],2);

    ok, i replaced it with that, but it only takes const char *, and I'm trying to write a float value....................and wtf is the second parameter for? lol

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    savefile.write(reinterpret_cast< const char* >( &level[a][b] ), sizeof( **level ) );

    or, rather than looping, you can simply do

    savefile.write(reinterpret_cast< const char* >( level ), sizeof( level ) );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary comparison from 2 files
    By Filly in forum C Programming
    Replies: 6
    Last Post: 01-16-2008, 06:49 AM
  2. Reading Binary files
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2005, 06:48 AM
  3. Writing to files
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 06-21-2005, 11:26 AM
  4. Binary Files
    By Axpen in forum C Programming
    Replies: 14
    Last Post: 08-12-2004, 08:41 PM
  5. Opening files in binary mode
    By ammar in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2002, 04:14 PM