Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone fix my code based on these errors? These are the errors: Here is my code: pokemon.h struct pokemon { int level; char name[25];

Can someone fix my code based on these errors?

These are the errors:

image text in transcribed

Here is my code:

pokemon.h

struct pokemon { int level; char name[25]; };

iofunctions.h

int readfile( struct pokemon pokearray[ ], int* num, char filename[ ] ); int writefile( struct pokemon pokearray[ ], int num, char filename[ ] );

iofunctions.c (Please fix this one)

#include #include #include #include "iofunctions.h" #include "pokemon.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 { 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 { fprintf(fptr,"%d ",pokearray[i].data); /*write data to file*/ fprintf(fptr,"%s ",pokearray[i].name);/*write name to file*/

} return 1;/*means writing the file is successful*/

}

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 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");

}

}

}

In file included from iofunctions.c:23:0: iofunctions.h:19:30: error: array type has incomplete element type int readfile( struct pokemon pokearray[ ], int* num, char filename[ ] ); iofunctions.h:19:22: warning: 'struct pokemon' declared inside parameter list [enabled by default] int readfile( struct pokemon pokearray[ ], int* num, char filename[ ] ); iofunctions.h:19:22: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default] iofunctions.h:20:31: error: array type has incomplete element type int writefile( struct pokemon pokearray[ ], int num, char filename[ ] ); iofunctions.h:20:23: warning: 'struct pokemon' declared inside parameter list [enabled by default] int writefile ( struct pokemon pokearray[ ], int num, char filename[ ] ); iofunctions.c: In function 'readfile': iofunctions.c:52:4: error: ISO 690 forbids mixed declarations and code [-Wpedantic] int count =0; /*denotes number of pokemons*/ iofunctions.c:62:25: error: 'struct pokemon' has no member named 'data' pokearray[count-1].data=data; /*store the data in array*/ iofunctions.c: In function 'writefile': iofunctions.c:80:4: error: 'for' loop initial declarations are only allowed in 099 mode for (int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions