I try to store input from a text file to an array of struct's elements
For example:
#001 Bulbasaur Grass Poison
#002 Ivysaur Grass Poison
#003 Venusaur Grass Poison
#004 Charmander Fire
#005 Charmeleon Fire
#006 Charizard Fire Flying
You can see that pokemons may have 2 types and some have only 1 type and the code went well until a pokemon that has only 1 type.
This a part of the output when I try to print the array of struct's elements:
#001 Bulbasaur Grass Poison
#002 Ivysaur Grass Poison
#003 Venusaur Grass Poison
#004 Charmander Fire #005
Charmeleon Fire #006 Charizard
Fire Flying #007 Squirtle
Water #008 Wartortle Water
#009 Blastoise Water #010
I tried a lot of things from checking a '\n' in the type1 buffer until searching web but I can't find a solution, please help me.
And BTW if I am doing something wrong with the dynamic memory allocation I would love to get suggestions.
These are the structs and the function:
typedef struct pokemon
{
int ID;
char *number;
char *name;
char *type1;
char *type2;
} Pokemon;
struct Trainer
{
char *firstName;
char *lastName;
Pokemon *pokemonsInBank;
Pokemon pokemonHeldByTheTrainer[6];
} Trainer;
struct pokemonDataBase
{
Pokemon *pokemonsDB;
int numOfPokemon;
} pokemonDataBase;
Storing Function
void loadPokemonsToDB()
{
char numBuffer[maxSize];
char nameBuffer[maxSize];
char typeBuffer[maxSize];
char type2Buffer[maxSize];
int i = 0;
char check[maxSize];
FILE *fp = fopen("Pokemons.txt", "r");
if (fp == NULL)
{
printf("Error opening the file\n");
exit(4);
}
pokemonDataBase.pokemonsDB = (Pokemon *)malloc((countPok()) * sizeof(Pokemon));
if (pokemonDataBase.pokemonsDB == NULL)
{
printf("Memory Error\n");
exit(4);
}
for (int i = 0; i < countPok(); i++)
{
fscanf(fp, "%s", numBuffer);
pokemonDataBase.pokemonsDB[i].number = (char *)malloc((strlen(numBuffer) + 1) * sizeof(char));
if (pokemonDataBase.pokemonsDB[i].number == NULL)
{
for (int j = 0; j < i; j++)
{
free(pokemonDataBase.pokemonsDB[j].number);
}
printf("Memory Error at index %d", i);
exit(4);
}
strcpy(pokemonDataBase.pokemonsDB[i].number, numBuffer);
fscanf(fp, "%s", nameBuffer);
pokemonDataBase.pokemonsDB[i].name = (char *)malloc((strlen(nameBuffer) + 1) * sizeof(char));
if (pokemonDataBase.pokemonsDB[i].name == NULL)
{
for (int j = 0; j < i; j++)
{
free(pokemonDataBase.pokemonsDB[j].name);
}
printf("Memory Error at index %d", i);
exit(4);
}
strcpy(pokemonDataBase.pokemonsDB[i].name, nameBuffer);
fscanf(fp, "%s", typeBuffer);
pokemonDataBase.pokemonsDB[i].type1 = (char *)malloc((strlen(typeBuffer) + 1) * sizeof(char));
if (pokemonDataBase.pokemonsDB[i].type1 == NULL)
{
for (int j = 0; j < i; j++)
{
free(pokemonDataBase.pokemonsDB[j].type1);
}
printf("Memory Error at index %d", i);
exit(4);
}
strcpy(pokemonDataBase.pokemonsDB[i].type1, typeBuffer);
fscanf(fp, "%s", type2Buffer);
pokemonDataBase.pokemonsDB[i].type2 = (char *)malloc((strlen(type2Buffer) + 1) * sizeof(char));
if (pokemonDataBase.pokemonsDB[i].type2 == NULL)
{
for (int j = 0; j < i; j++)
{
free(pokemonDataBase.pokemonsDB[j].type2);
}
printf("Memory Error at index %d", i);
exit(4);
}
strcpy(pokemonDataBase.pokemonsDB[i].type2, type2Buffer);
}
}