In: Computer Science
Create a header file, iofunctions.h, containing the following function prototypes. (don't forget to follow the coding style)
int readfile( struct pokemon pokearray[ ], int* num, char filename[ ] ); int writefile( struct pokemon pokearray[ ], int num, char filename[ ] );
Then, create a source file, iofunctions.c, defining the functions above.
Specifications
The readfile function will read the data from a text file and store it in the array called by pokearray.
It must not load the pokemons more than the array can hold. If the array size is 10, the function must not load more than 10 pokemons.
It must tell the function which called readfile how many pokemons it has stored into the array.
The writefile function will write the data stored in the array into a text file.
If the text file already exits with some data, writeifile must OVERWRITE it with the newly given data. For example, if the file has one pikachu and the array has a charizard and a bulbasaur, the file will only have a charizard and a bulbasaur after writefile function is called.
The return values must be used to tell whether the functions successfully read/wrote data.
The size of the array and the number of pokemons in the text file are assumed to be arbitrary (You cannot assume the array size is always 5).
The file name of the text file is also arbitrary.
The text file must be just a sequence of items, i.e., it must not tell how many pokemons it contains at the beginning.
For example, the contents in a file should look like this:
16 Pikachu 9 Metapod 70 Mewtwo
They must not crash (segmentation fault) even if the file does not exist or cannot be open. (first run, some problem in the file system, etc)
Reminder: readfile and writefile functions must not have the terminal I/O's (printf, scanf, etc).
The code consists of two files iofunctions.h and iofunctions.c
iofunctions.h==>this header file contains structure pokemon and prototypes of readfile and writefile functions.....
readfile function has three agrument==>
(i) pokearray[ ] ==>this parameter is used to store the data read from the file
(ii) num ==> which is a reference to the variable in main function and hence store the number of pokemons stored in the array..
(iii) filename[] ==> which stores the filename from which data is to be read
writefile function has three agrument==>
(i) pokearray[ ] ==>this parameter has data stored which is used to write data to file
(ii) num ==> which is a variable that tells the number of pokemons in the array
(iii) filename[] ==> which stores the filename on which data is to written
From main function the input of maxsize and input of file name to be read and wriiten is taken ..
The code snippet and screen shot has been attached
iofunctions.h
#include<stdio.h>
//Declaring the structure pokemon
struct pokemon{
char name[50];//name of pokemon
int data;//data associated with it
};
int readfile(struct pokemon pokearray[],int *num,char filename[]);
int writefile(struct pokemon pokearray[],int num,char filename[]);
iofunctions.c
#include <stdio.h>
#include <stdlib.h>
#include "iofunctions.h"
#include<string.h>
int max_size;
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.
}
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<max_size&&fscanf(fptr,"%d",&data)!=EOF)
{
count++; //increase the count of pokemon
fscanf(fptr,"%s",name); //read the name from file
pokearray[count-1].data=data; //store the data in array
strcpy(pokearray[count-1].name,name);//store the name in array
}
*num=count;//store the count in num
return 1;//means reading the file is successful
}
int writefile(struct pokemon pokearray[],int num,char filename[])
{
FILE *fptr; //delcare the file pointer
//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(int i=0;i<num;i++)
{
fprintf(fptr,"%d\n",pokearray[i].data); //write data to file
fprintf(fptr,"%s\n",pokearray[i].name);//write name to file
}
return 1;//means writing the file is successful
}
int main()
{
printf("Enter the max size of array \n");
scanf("%d",&max_size);//take input of max size of array
struct pokemon *pokearray =(struct pokemon*)(malloc)(sizeof(struct pokemon)*max_size);//allocate space
int num=0;//intialize num variable
printf("Enter the file to be read\n");
char filename[50];
scanf("%s",filename);//input the file name to be read
if(readfile(pokearray,&num,filename)==0) //call the readfile function
{
printf("Error in reading the file\n"); //error
}
else
{
printf("File read successfully\n %d pokemons has been stored in the array\n",num);
printf("Enter the name of the file on which data is to be written\n");
scanf("%s",filename);//input the file name of output file
if(writefile(pokearray,num,filename)==0)
{
printf("Error in writing the file\n");//error
}
else
{
printf("Data has been written successfully");
}
}
}
Screenshots