In: Computer Science
Download “dict.h” and “dictionary.c” posted on blackboard.
a) read both code to figure out what they do.
b) supply am appropriate content to the main function in
“dictionary.c” to demonstrate
that you understand the provided code by invoking those functions
defined in
“dictionary.c”
Submit your source code and script files.
/* dict.h header for data dictionary routines. */
#include <stdio.h>
struct dict_elem{
char d_name[15]; // name of dictionary member
int d_start; //starting position in record
int d_length; //length of field
int d_type; //denotes type of data
};
#define ERROR (-1)
#define SUCCESS 0
//dictionary.c
#include "dict.h"
int writedict(const char *dictname, struct dict_elem *elist){
int j;
FILE *outf;
if ((outf = fopen(dictname, "w")) == NULL){
return ERROR;
}
//cculate length of the aray
for (j = 0; elist[j].d_length != 0; j++)
;
//write out list of dict_elem structures
if (fwrite((void*)elist, sizeof(struct dict_elem), j,
outf)<j){
fclose(outf);
return ERROR;
}
fclose(outf);
return SUCCESS;
}
//**************************************************************************
struct dict_elem * readdict(const char *dictname,
struct dict_elem *inlist, int maxlength)
{
int i;
FILE *inf;
if ((inf = fopen(dictname, "r")) == NULL){
return NULL;
}
//read in dict_elem structures from file
for (i = 0; i < maxlength - 1; i++)
{
if (fread((void*)&inlist[i],
sizeof(struct dict_elem), 1, inf)<1)
{
break;
}
}
fclose(inf);
//mark end of list
inlist[i].d_length = 0;
return inlist;
}
// your main function goes here.
int main(void)
{
// struct dict_elem array[3] ={ {}, {},{} , {} }
}
/* dict.h header for data dictionary routines. */
#include <stdio.h>
struct dict_elem
{
char d_name[15]; // name of dictionary member
int d_start; //starting position in record
int d_length; //length of field
int d_type; //denotes type of data
};
#define ERROR (-1)
#define SUCCESS 0
---------------------------------------------------------------------
//dictionary.c
#include "dict.h"
int writedict(const char *dictname, struct dict_elem *elist){
int j;
FILE *outf;
if ((outf = fopen(dictname, "w")) == NULL){
return ERROR;
}
//cculate length of the aray
for (j = 0; elist[j].d_length != 0; j++)
;
//write out list of dict_elem structures
if (fwrite((void*)elist, sizeof(struct dict_elem), j,
outf)<j){
fclose(outf);
return ERROR;
}
fclose(outf);
return SUCCESS;
}
//**************************************************************************
struct dict_elem * readdict(const char *dictname, struct dict_elem
*inlist, int maxlength)
{
int i;
FILE *inf;
if ((inf = fopen(dictname, "r")) == NULL){
return NULL;
}
//read in dict_elem structures from file
for (i = 0; i < maxlength - 1; i++)
{
if (fread((void*)&inlist[i], sizeof(struct dict_elem), 1, inf)
< 1)
{
break;
}
}
fclose(inf);
//mark end of list
inlist[i].d_length = 0;
return inlist;
}
// your main function goes here.
int main(void)
{
int c;
// Creates dictionary name
char *dictname = "MyDictionary";
// Creates an array of dict_elem
struct dict_elem array[3] ={ {"Animal", 0, 4, 1}, {"Book", 2, 5,
2}, {"Bird", 1, 5, 3}};
// Calls the function to read data and checks if not SUCCESS
displays error message
if(writedict(dictname, array) != SUCCESS)
printf("\n Unable to write");
// Creates a temporary object of dict_elem
struct dict_elem temp;
// Creates a pointer object pointing to temp
struct dict_elem *inlist = &temp;
// Calls the function to read data from file
inlist = readdict(dictname, &inlist[0], 4);
// Checks if inlist is not null
if(inlist != NULL)
{
// Loops 3 times
for(c = 0; c < 3; c++)
// Displays current object information
printf("\n\n Name: %s \n Start: %d \n Length: %d \n Type:
%d",
inlist[c].d_name, inlist[c].d_start, inlist[c].d_length,
inlist[c].d_type);
}
// Otherwise displays error message
else
printf("\n Unable to redirect");
return 0;
}
Sample Output:
Name: Animal
Start: 0
Length: 4
Type: 1
Name: Book
Start: 2
Length: 5
Type: 2
Name: Bird
Start: 1
Length: 5
Type: 3