writing to binary files....

This is a discussion on writing to binary files.... within the C++ Programming forums, part of the General Programming Boards category; how do i write to a binary file without it thinking that I'm writing it as a normal file? ofstream ...

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904

    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
    University of Waterloo
    Posts
    1,904
    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, 07:48 AM
  3. Writing to files
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 06-21-2005, 12:26 PM
  4. Binary Files
    By Axpen in forum C Programming
    Replies: 14
    Last Post: 08-12-2004, 09:41 PM
  5. Opening files in binary mode
    By ammar in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2002, 05:14 PM

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