Question: Can someone create a main function file to test my writefile and readfile ? Create a source file, driver.c , defining the following local variables
Can someone create a main function file to test my writefile and readfile?
Create a source file, driver.c, defining the following local variables in the main function:
struct pokemon pokemonbank[5]; int numpokemons;
-
-
You may get input from the keyboard or hardcode your testing data in the driver code.
-
The output of the program must be clear and self-explanatory (DO NOT just print like "The functions worked perfectly.", "SUCCESS", etc)
-
Test cases and results must be definitive proof that the functions work; make sure that your test cases can really prove that the functions work. (critical thinking!, e.g., "What if the function was totally broken and it did not access the array and the file at all? Would the results be different from the successful case?")
-
-
The driver code MUST NOT directly read or write a file.
Here is my Readfile/ Writefile :
#include#include #include int max_size; struct pokemon { int level; char name[25]; }; int readfile(struct pokemon pokearray[],int *num,char filename[]) { FILE *fptr; /*file pointer*/ /*initialize the file pointer.. if it returns NULL then error must be there in reading file */ if ((fptr = fopen(filename, "r")) == NULL) { return 0; /*unsuccessful reading of file */ /* function returns 0 if pointer returns NULL. */ } /* * We begin a new segment, to hold the new variables. */ { int count =0;/* denotes number of pokemons */ int data; /* number attached to each pokemon */ char name[50]; /* name of pokemon */ /* check if count is less than max size ..check whether file has reached end of line or not */ /* if not EOF then data get the data stored with pokemon */ while(count ){ count++; /* increase the count of pokemon */ fscanf(fptr,"%s",name); /* read the name from file */ /* TODO: here, nothing has been assigned to data, so commenting out the next line * else compiler throws warning-error. */ /*pokearray[count-1].level=data; */ strcpy(pokearray[count-1].name,name);/* store the name in array */ } *num=count;/* store the count in num */ } fclose(fptr); return 1;/* means reading the file is successful */ } int writefile(struct pokemon pokearray[],int num,char filename[]) { FILE *fptr; /* delcare the file pointer */ int i = 0; /* for loop-indexing */ /* initialize the file pointer.. if it returns NULL then error must be there in writing file */ if ((fptr = fopen(filename, "w")) == NULL) { return 0; /* function returns 0 if file pointer returns NULL. */ } for(i=0;i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
