Question regarding working with binary file

Posted on 2006-05-09
Last Modified: 2010-04-15
Hi all,
  I am hoping someone here can help me out.  Basically what I am trying to do is manipulate a binary .dat file throughout an application.  but I want to be able to open the file at the beginning (through a call to an external function) and then continue to pass through the FILE pointer from function to function.  Like I have a function to open the file, another to read from it, another to write to it, and finally one to close it.  My problem is, I can't figure out what syntax to use to pass this FILE pointer back and forth.  

here is the MAIN:

int main(int argc, char *argv[])
  FILE  *cd_file;
  int   fileopen;  /* boolean*/
  int   count;
  cds   my_cd;
  case 1:
     fileopen = 0;
  case 2:
     fileopen = open_read(argv[1], cd_file);  /* this works and successfully passes back fileopen, but i don't seem to get back the File pointer correctly  <----------- */
  count = read_binary_file(cd_file);  /* this doesn't work at all.  would also like to be able to get back what was read. can i do that?  <------------------------ */
  printf("successful close");
  return 0;

And here is the database.h file:

#include <stdio.h>
#define TITLE_MAX 50
#define ARTIST_MAX 30

typedef struct cd_info
   char   title[TITLE_MAX];
   char   artist[ARTIST_MAX];
   int    tracks;
   int    album;  /* boolean value */
   float  price;
} cds;

/* open a file for reading */

int open_read(char *filename[], FILE *cd_file)
   int   fileopen = 0;  /* boolean */
   cd_file = fopen(filename, "rb");
   if(cd_file == NULL)
      puts("The file you specified could not be opened.\n");
      fileopen = 0;
      fileopen = 1;
      printf("successful open\n");
   return fileopen;
/* read in the contents of a binary file */

int read_binary_file(FILE *fptr)  
    int  count;
    cds  one_cd;
    count = fread(&one_cd, sizeof one_cd, 300, fptr);  /* would also like to pass back what was read as well (&one_cd?) and would that be an array within main when passed/ */
   return count;

So basically, i am looking for a way to get the file pointer correctly passed back to main from open_read() so that i can then correctly pass it into read_binary_file().  Can i do this?  Thanks.
Question by:drewman75
    LVL 45

    Accepted Solution

    Hi drewman75,

    Your file stream (FILE object) is a pointer to a structure.  So you'll pass it just like you'd pass any other pointer.

    FILE * open_read (char *string, FILE *file)
    //  do something
      return file;

    main ()
      FILE *cd_file;

      cd_file = open_read (argv[1], cd_file);

    Keep in mind that you'll want to capture the return value only the first time the function is called as that's the only time that you'll open the file.  It won't hurt to capture it every time, but that's simantically incorrect.

    Better yet is to call the system's fopen() function to start the process and not worry about a return value.

    Good Luck!

    Author Comment

    That worked like a champ.  Thank you so much.  Accepting your answer and awarding points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now