ofstream savefile;
savefile.open("DEFAULT.B3D",ios::out | ios::binary | ios::trunc);
for(int a=0;a<30;a++)
{
for(int b=0;b<30;b++)
{
savefile <<''a'';
}
}
savefile.close();
writing to a binary file
I am trying to write to a binary file....but it''s just writing it as if it were being opened normally...here''s the code I''m trying to use:
What exactly are you trying to do... in a binary file... the letter a is represented with the letter a
. So, it WILL just look like a bunch of letter a''s if you view the file, whether you wrote it out using binary or text makes no difference.

sorry, should have been more concise, I'm trying to output a matrix:
and it's just outputting a bunch of numbers.....I know there's a way to save each number to its own byte, and not be displayed as a true number, but how?
[edited by - jverkoey on February 9, 2003 11:14:16 PM]
float level[30] [30];savefile << level[a] [b];
and it's just outputting a bunch of numbers.....I know there's a way to save each number to its own byte, and not be displayed as a true number, but how?
[edited by - jverkoey on February 9, 2003 11:14:16 PM]
I normally use fopen, fclose, fwrite, fread, fprintf, and fscanf for my file functions, they always work how I want them to
. I think I understand your problem, you want it to output the 4 bytes representing the float, but it's outputing the actual floating point number? I am not sure of how to get around that using ofstreams, but I can show you how using stdio (plain old C) functions.
-- Edit --
Ok, he just edited his post, so now mine makes less sense because he just explained the same problem I just said I assumed... but, I assumed it before I got his Edit
.
[edited by - Ready4Dis on February 9, 2003 11:15:27 PM]

FILE *outfile;outfile = fopen("Default.b3d","wb"); //Open for write binary!if (outfile) //Check to see if it opened{ fwrite(level,sizeof(float),30*30,out); //Write our 30x30 floating point level out! fclose(outfile); //Close our file, we're done!}else //Error creating/opening file!{}
-- Edit --
Ok, he just edited his post, so now mine makes less sense because he just explained the same problem I just said I assumed... but, I assumed it before I got his Edit

[edited by - Ready4Dis on February 9, 2003 11:15:27 PM]
ok, that works fine and dandy, but what's all the extra stuff at the end of the file?
-EDIT-
ERK, nevermind, I'm stupid, lol, I had evidently changed the value of the matrix to char for some reason.....lol, changed it back to float and now it works fine
[edited by - jverkoey on February 9, 2003 11:25:21 PM]
-EDIT-
ERK, nevermind, I'm stupid, lol, I had evidently changed the value of the matrix to char for some reason.....lol, changed it back to float and now it works fine
[edited by - jverkoey on February 9, 2003 11:25:21 PM]
February 09, 2003 10:27 PM
You unfortunately can''t use the overloaded operators to write to a stream in this way:
savefile << 1.0f << std::endl;
will write out to the file f in text format.
Try looking in to the f.read and f.write functions, as I think that is what you are looking for and are similar to the C functions shown above...
savefile << 1.0f << std::endl;
will write out to the file f in text format.
Try looking in to the f.read and f.write functions, as I think that is what you are looking for and are similar to the C functions shown above...
Well you can still use the iostreams...
When you open the file in binary the only thing that changes is some parsing of newlines. However it does not change the << and >> operators. In order for it to put the character and not the numerical value you need to use the put(), write(), get(), and read() funtions of the iostream you should not use the overloaded operators. for more info go here http://www.ccd.bnl.gov/bcf/cluster/pgi/pgC++_lib/stdlib.htm
Hope that helps =)
When you open the file in binary the only thing that changes is some parsing of newlines. However it does not change the << and >> operators. In order for it to put the character and not the numerical value you need to use the put(), write(), get(), and read() funtions of the iostream you should not use the overloaded operators. for more info go here http://www.ccd.bnl.gov/bcf/cluster/pgi/pgC++_lib/stdlib.htm
Hope that helps =)
ASCII stupid question, get a stupid ANSI
What extra stuff at the end of the file? There shouldn't be any
. I beleive "wb" should delete the old file, and create a new one (wiping out any info in that file) before it opens it for writing, so there shouldn't be any extra stuff at the end of the file. Are you declaring your level as float level[30][30];? You might want to try something like this instead if you can't get that to work:
-- Edit --
He once again did it to me, making an edit saying it was his fault, and that it was working
. I'll leave this up for no reason in particular.
[edited by - Ready4Dis on February 9, 2003 11:30:24 PM]

FILE *outfile;outfile = fopen("Default.b3d","wb"); //Open for write binary!if (outfile) //Check to see if it opened{ int a; for (a=0;a!=30;++a) { fwrite(level,sizeof(float),30,out); //Write out a line of 30 floats } fclose(outfile); //Close our file, we're done!}else //Error creating/opening file!{}
-- Edit --
He once again did it to me, making an edit saying it was his fault, and that it was working

[edited by - Ready4Dis on February 9, 2003 11:30:24 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement