|
-
Sep 21st, 2002, 02:31 PM
#1
Thread Starter
Addicted Member
Convert float vertices to binary file?
Hi,
I can read and write vertices (floats) using ASCII files. I can even read vertices doing a binary read. How do I take some float values and write them to a binary file? A simple example writing a number such as -12.34 would be appreciated. In this case, I think a float takes 4 bytes...so the file would be that long.
I want to save my 3D model data in binary format for quicker loading.
Regards,
ChuckB
-
Sep 21st, 2002, 05:21 PM
#2
Frenzied Member
Easy one =).
Code:
...
FILE * f = fopen("myDrive:\\myDir\\myFile.myFileExtension", "w+b");
float someFloat = -12.34f;
fwrite(&someFloat, 1, sizeof(float), f);
fclose(f);
...
Z.
-
Sep 21st, 2002, 07:28 PM
#3
Thread Starter
Addicted Member
Z,
I knew I could count on your help. :-)
Here is another question. Most samples of file writing and reading that I see tend to be good ole C. I don't see too many C++ examples using iostreams...reading/writing binary and ascii files. I suppose, why bother, when the C format is so easy to use. However, I do want to learn these other formats for file streaming.
Regards,
ChuckB
-
Sep 21st, 2002, 07:41 PM
#4
Frenzied Member
Never used fstreams for binary file IO, but they are quite easy to use:
Code:
#include <fstream>
using namespace std;
main()
{
ofstream of("myDrive:\\myDir\\myFile.myFileExtension");
of << "Hello!" << endl;
of.close();
return 0;
}
That is the beauty of streams... consider this code:
Code:
#include <fstream>
using namespace std;
void output(ostream& os)
{
os << "Hello" << endl;
}
main()
{
output(cout);
ofstream of(...);
output(os);
os.close();
return 0;
}
You can also inherit from the ostream class to create your own output streams and such.
Z.
-
Sep 22nd, 2002, 04:53 AM
#5
Monday Morning Lunatic
For binary, they're very similar in use to the C method:
Code:
#include <fstream>
void somecode() {
ofstream fil("output.bin", ios::binary);
char dat[256];
fil.read(dat, 256); // i think, might need a sizeof in there somewhere.
}
I need my reference back
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|