Thread: Binary File

  1. #1
    Bob Dylan Lover
    Guest

    Binary File

    I need to write some data to a binary file.

    What is the proper fopen() arguments for opening a binary file for writing, and which function do I use to write to the file? fgets() or fwrite()?

    I am writing a structure of data to file.

    struct frameInfo {
    float ssmTime;
    int data;
    };

    struct frameInfo frame[16];


    A little help with the arguments for doing this would be appreciated.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    FILE *fp = fopen( "file", "wb" );
    fwrite( frame, sizeof(struct frameInfo), 16, fp );

    This should write the whole array in one simple step

  3. #3
    Bob Dylan Lover
    Guest

    Compile error

    Thanks. The fwrite() command gave me a compiler error. Do I need to type cast the first argument?

    error C2664: 'fwrite' : cannot convert parameter 1 from 'struct frameInfo' to 'const void *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

  4. #4
    Bob Dylan Lover
    Guest

    & cleared it up

    &frame


    Thanks for the help!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    > &frame
    Wait a minute, I thought you were writing an array of these, not a single instance.

    Hope you replaced 16 with 1 in my example, otherwise you're writing too much data

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM