output to a binary file

I originally posted the wrong issue. This is the true issue I have been looking over for the last few hours. Thanks for looking. Any help will be credited :)


Write a program that will output two integers then two floats to the data file L18out.bin


*/





#include < stdio.h>

int main(void)
{



/* Create an integer and a float then initialize them */
int myint1 = 77;
int myint2 = 20;
float myfloat1 = 1.1;
float myfloat2 = 3.14;

/* Open .bin file for append */
FILE * open; /* Create a FILE pointer called 'open' */

open = fopen("L18out.bin", "a");

/* Test the FILE pointer
if it equals NULL the file did not open
if it does not equal zero the file opened */

if (open == NULL)
{

/* If we got here, the file did not open
tell the user and exit. */

printf("The file could not be opened.
"); // change /n to backslash n


}

else

{

/* If we got here, the file is open and we can
add data to the file. */

/* fseek moves the file pointer
open - FILE pointer
0 - How far to move
SEEK_SET - Start at beginning of the file
SEEK_CUR - Move from current pointer position
SEEK_END - Move from the end of the file */

fseek(open, 0L, SEEK_SET);

fprintf (open, myint1 );

fprintf (open, myint2 );

fprintf (open, myfloat1 );

fprintf (open, myfloat2 );

fclose(open);
}


return 0;
}

Comments

  • [color=Blue]Looks OK - what is not working here?[/color]
    :
    : [code]
    : #define LINE '
    '
    : #define MAX 81
    :
    : int main (void)
    : {
    :
    : int count=0; //not sure if I need a control variable here or
    : not. I used one anyway
    : char container[MAX];
    : char * ptr = container; //array pointer
    :
    :
    : printf ("I'm rather bloated these days, I can only hold 80
    : characters:

    ");
    :
    : gets(container); //places string into 'container' array
    :
    : while (count < MAX && *ptr != '')
    : {
    :
    : putchar (*ptr++); //trying to make just a single part of the
    : array print to screen
    : putchar (LINE); //used putchar for greater ease of reading.
    : this intentionally points out the line return requirement
    : count++;
    : }
    :
    :
    : printf ("
    I never said you could hit that button,
    but I
    : suppose the button 'Purge' means something different to you.
    ");
    :
    : //Once again I attempt robot humor a la Hitchiker, but only to keep
    : myself amused.
    :
    :
    :
    : return 0;
    : }
    : [/code]
    :
  • : I originally posted the wrong issue. Sorry.


    I also have to edit the function above for the final part of this project. I need to read the input from the file from the above binary file. Here is what I have so far:

    #include < stdio.h>

    int main(void)
    {





    /* Open .bin file for append */
    FILE * open; /* Create a FILE pointer called 'open' */

    open = fopen("L18out.bin", "a");

    /* Test the FILE pointer
    if it equals NULL the file did not open
    if it does not equal zero the file opened */

    if (open == NULL)
    {

    /* If we got here, the file did not open
    tell the user and exit. */

    printf("The file could not be opened.
    "); // change /n to backslash n


    }

    else

    {

    /* If we got here, the file is open and we can
    add data to the file. */

    /* fseek moves the file pointer
    open - FILE pointer
    0 - How far to move
    SEEK_SET - Start at beginning of the file
    SEEK_CUR - Move from current pointer position
    SEEK_END - Move from the end of the file */

    fseek(open, 0L, SEEK_SET);


    // This is where i would probably use fread() but i think my arguements are the problem.

    }


    return 0;
    }
  • i editted the information after 'else'. I might try to put a sizeof command in here somewhere to get the number of places that were written. I am hoping to use this module in combination with another that reads the file created by this one.

    ...

    else

    {

    /* If we got here, the file is open and we can
    add data to the file. */

    /* fseek moves the file pointer
    open - FILE pointer
    0 - How far to move
    SEEK_SET - Start at beginning of the file
    SEEK_CUR - Move from current pointer position
    SEEK_END - Move from the end of the file */

    fseek(open, 0L, SEEK_SET);

    fprintf (open, "%d", myint1 );

    fprintf (open, "%d", myint2 );

    fprintf (open, "%f", myfloat1 );

    fprintf (open, "%f", myfloat2 );

    fclose(open);
    }

    printf ("You made it to the end without bombing.

    ");
    //this is just to make sure the app finishes with no issues.

    return 0;
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories