Guides:C/C Crash Course/Adding write capability to phonebook.c

From CoderGuide

Jump to: navigation, search

Back to TOC

Adding write capability to phonebook.c

Now that we have covered the basic file I/O commands (and some not so basic), we're going to improve on our phone book application so that we can save files to the disk:

First, make a copy of phonebook.c and name it as phonebook2.c (or whatever you like, and get ready to make a few changes.

You'll want to change the initial comment in phonebook2.c to read something like:

/*******************
phonebook2.c -- an improved phone book program, with file support.
********************/

We're going to need two new functions to load and save the database, so add these two function prototypes with the others in your program:

void savebook();
void readbook();

Next, we will want a way for the user to use these new functions, so add two more lines to our display code right between "(L)ist entries (brief)" and "(Q)uit" as shown below:

                puts("(S)ave database");
                puts("(R)ead database");

Now we need to add a few lines to the switch() statement code to recognize these new options. Adding them right after the line containing switch(cmd){ is a good place.

                        case 'S':
                        case 's': savebook(); break;
 
                        case 'R':
                        case 'r': readbook(); break;

Now that the user interface is finished, let's write our file access functions. Remember when we talked about the sizeof keyword? We're going to need to use it here. The sizeof statement is effectively replaced by the size, in bytes, of whatever the data type that comes after it. So, the statement:

printf("%d\n",sizeof char);

Should print 1 on your screen, because a char is one byte in length. However:

printf("%d\n", sizeof int);

May print 2, 4, even 8 or more, depending on your system and compiler. Now lets write our file access functions:

/****************
 ** readbook() **
 ****************/
void readbook(){
        FILE *fp;
        char name[256];
 
        printf("File to read:");
        gets(name);
 
        /*add our custom extension*/
        strcat(name,".phn");
 
        fp=fopen(name,"rb");
        if(!fp){
                fprintf(stderr,"ERROR: Can't open %s\a\n",name);
                return;
        }
        fread(records,sizeof (struct Record),ENTRIES,fp);
        fclose(fp);
 
}/*end readbook()*/
 
/****************
 ** savebook() **
 ****************/
void savebook(){
        FILE *fp;
        char name[256];
 
        printf("File to write:");
        gets(name);
 
        /*add our custom extension*/
        strcat(name,".phn");
 
        /*overwrite, if file exists*/
        fp=fopen(name,"w");
 
        if(!fp){
                fprintf(stderr,"ERROR: Can't create %s\a\n",name);
                return;
        }
        fwrite(records,sizeof (struct Record),ENTRIES,fp);
        fclose(fp);
}/*end savebook()*/

And now we have our improved phone book!

But we can do better than that. We're going to rewrite our address book program so that we can have as many, or as few entries in our address book as we want, plus the ability to search and sort!

Personal tools