• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Question regarding working with binary file

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;
 
  switch(argc)
  {
  case 1:
     fileopen = 0;
     break;
  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?  <------------------------ */
 
  fclose(cd_file);
 
  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;
   }
   else
   {
      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.
0
drewman75
Asked:
drewman75
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
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!
Kent
0
 
drewman75Author Commented:
That worked like a champ.  Thank you so much.  Accepting your answer and awarding points.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now