Results 1 to 5 of 5

Thread: Convert float vertices to binary file?

  1. #1

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157

    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

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    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.

  3. #3

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    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

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    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.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width