Thread: Using fwrite to create a binary to specify fields within the record

  1. #1
    Registered User
    Join Date
    Apr 2002

    Using fwrite to create a binary to specify fields within the record

    The following is code that works and sends "size" to my output file. My question is ...

    I do want size to be outputted, but I also want additional information, such as fields from the struct....say rec.micr.
    How can I get multiple values/fields sent to the file at the same time(same record).
    Thanks for any suggestions ...

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <IO.h>
    struct fields {
    char micr[103];
    char seg1[5];
    char segx[7];
    char seg3[5];

    FILE *input,*output;
    char *buffer_input;
    char seg_len;
    int x, y, size;
    int z = 127;
    buffer_input = malloc(50000);
    if ((input = fopen("DD:IN","rb")) == NULL)
    printf("open of IN DD failed\n");
    printf("Successful open\n");
    printf("\nImmediately after open, postion = %ld",ftell(input));
    if ((output = fopen("DD:OUT",
    "wb,recfm=fb,lrecl=80,type=record")) == NULL)
    printf("open of OUT DD failed\n");

    printf("\nAfter det seg size postion = %d",ftell(input));
    printf("\nbuffer = %s",buffer_input);
    strncpy(rec.seg1,buffer_input + 103,5);
    strncpy(rec.seg3,buffer_input + 115,5);
    printf("\nSegment1 len = %s\n", rec.seg1);
    printf("\nSegment3 len = %s\n", rec.seg3);
    x = atoi ( rec.seg1);
    y = atoi ( rec.seg3);
    size = x + y + z;
    printf("\nSize of second image = %d", size);
    printf("\nImmediately after fseek postion = %ld",ftell(input));

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Write out the entire struct in one go, all fields:

    Is that what you mean?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002

    Before I answer your fwrite() question, consider using 'code tags' in future posts. Code tags format your code for easier reading, and can be included with the # button when posting.

    Prototype of fwrite():
    size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
    If you would like to write the contents of a structure to file using fwrite(), you need to specify where the instance of that structure is (ptr), the size of the structure (size), how many instances you want written (nitems), and which file to send it to (stream). Note that writing multiple items with one call to fwrite() requires the items to be contiguous in memory (an array of items).

    FILE *fptr;
    struct fields my_field;
    size_t rval;
     * Once you have opened your destination file and you are
     * ready to write your structure...
    rval = fwrite( &my_field, sizeof(my_field), 1, fptr );
     * Be sure to check for errors...
    if ( rval == -1 )
      /* Error */
      perror( "fwrite" );
    Hope that helps,
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  3. Replies: 3
    Last Post: 03-02-2008, 11:33 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
Website Security Test