I'm trying to make two functions, a writefile and a readfile. Obviously, writefile will write to a file using the contents from the struct pokemon, and readfile will read the file line by line and get the contents of the file. For example, after writing to a file successfully with two pokemon in the struct, the text file will look like this:
30
Pikachu
12
Charizard
The read file is supposed to store the level and name in the struct, and I'm currently having trouble. I've already been told that using fgets is correct, but I'm not doing it correctly.
struct pokemon
{
int level;
char name[30];
};
int readfile(struct pokemon pokearray[], int* num, char filename[])
{
FILE * fp;
int i = 0;
fp = fopen(filename, "r");
pokearray[10].level;
if (fp == NULL)
{
printf("errorrr");
return -1;
}
while (!feof(fp))
{
fgets(pokearray[i].level, 10, fp);
}
fclose(fp);
return 0;
}