Question
Can someone fix my code so it works on ISO C90? (I'm not allowed to convert to C99 mode) Also please fix the warning at
Can someone fix my code so it works on ISO C90? (I'm not allowed to convert to C99 mode) Also please fix the warning at the end of the part that reaches the end of a non-void function.
Here is my code:
#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. */ } 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 */ pokearray[count-1].level=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 } int main() { printf("Enter the max size of array "); 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 "); 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 "); /* error */ } else { printf("File read successfully %d pokemons has been stored in the array ",num); printf("Enter the name of the file on which data is to be written "); scanf("%s",filename);/* input the file name of output file */ if(writefile(pokearray,num,filename)==0) { printf("Error in writing the file ");/* error */ } else { printf("Data has been written successfully"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started