0 Replies - 3174 Views - Last Post: 25 February 2014 - 07:28 AM Rate Topic: -----

#1 jimblumberg  Icon User is offline

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,290
  • Joined: 25-December 09

C snippet to demonstrate binary file writing.

Post icon  Posted 25 February 2014 - 07:28 AM

A small snippet to demonstrate writing and reading a structure to a binary file.

/* Program to demonstrate binary file writing/reading of a structure. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAME_SIZE 21
#define ADDRESS_SIZE 51
#define MAX_CUSTOMERS 100

struct customer_rec
{
   int id;
   char first_name[NAME_SIZE];
   char last_name[NAME_SIZE];
   int age;
   char gender;
   char address[ADDRESS_SIZE];
};

typedef struct customer_rec Customer;

/* Function prototypes */
void printCustomers(FILE* infile, FILE* outfile);
void printCurrentCustomer(FILE* prn, Customer customer);
int writeCustomerBin(FILE* fpcustbin, Customer *customer, int counter);
void initCustomer(Customer *customers, int *number_of_customers);

int main()
{
   /* declaring variables */
   FILE *fpcustbin;
   /* Create an array of Customer to hold the information. */
   Customer customer[MAX_CUSTOMERS];
   int error_num;
   int num_customers;

   /* Initialize some customer structures. */
   initCustomer(customer, &num_customers);

   /* Opening the ouput file, erasing current contents. */
   fpcustbin = fopen("customers.bin","w+b");
   if(fpcustbin == NULL)
   { /* File opening failed, inform user and exit program. */
      puts("Error opening customers.bin");
      return(1);
   }

   if((error_num = writeCustomerBin(fpcustbin,customer, num_customers)) != 0)
   { /* Writing to file failed for customer indicated by error_num */
      printf("Error writing customer: %d", error_num);
      return(2);
   }
   /* Print the customers contained in the binary file. */
   printCustomers(fpcustbin, stdout);

   fclose(fpcustbin);

   return(0);
}

int writeCustomerBin(FILE* fpcustbin, Customer *customer, int num_customers)
{
   int test;
   int i = 0;

   for(; i < num_customers; ++i)
   { /* Write the customer information to the file, one record at a time. */
      test = fwrite(&customer[i], sizeof(Customer),1, fpcustbin);
      if(test != 1)
         return(i);
   }
   return(0);
}

void printCustomers(FILE* ifile, FILE* ofile)
{
   Customer customer;

   /* Put the file pointer back at the beginning of the file.
      Also resets all FILE errors if any are present.*/
   rewind(ifile);

   /* Read and print the customer records one at a time. */
   while(fread(&customer, sizeof(Customer), 1, ifile) == 1)
   {
      printCurrentCustomer(ofile, customer);
   }
}

void printCurrentCustomer(FILE* prn, Customer customer)
{
   fprintf(prn, "ID: %d\n", customer.id);
   fprintf(prn,"Name: %s %s\n", customer.first_name, customer.last_name);
   fprintf(prn,"Age: %d\n", customer.age);
   fprintf(prn,"Gender: %c\n", customer.gender);
   fprintf(prn,"Address: %s\n\n", customer.address);
}

void initCustomer(Customer *customers, int *number_of_customers)
{
   /* Populate array with several entries for demonstration. */
   *number_of_customers = 3;

   customers[0].id = 1003;
   strcpy(customers[0].first_name, "John");
   strcpy(customers[0].last_name, "Jones");
   customers[0].age = 80;
   customers[0].gender = 'M';
   strcpy(customers[0].address, "21 Main St.");

   customers[1].id = 1004;
   strcpy(customers[1].first_name, "Sally");
   strcpy(customers[1].last_name, "Jones");
   customers[1].age = 75;
   customers[1].gender = 'F';
   strcpy(customers[1].address, "21 Main St.");

   customers[2].id = 1001;
   strcpy(customers[2].first_name, "Abigail");
   strcpy(customers[2].last_name, "Adams");
   customers[2].age = 20;
   customers[2].gender = 'F';
   strcpy(customers[2].address, "34 Summer Lane");
}



Jim

Is This A Good Question/Topic? 2
  • +

Page 1 of 1