Think of this program like a Pokédex app where you can:
#include <stdio.h> // For input/output (printf, scanf)
#include <stdlib.h> // For system functions (like system("cls"))
#include <string.h> // For string functions (though not used much here)Analogy: Think of these like importing tools into your workshop. stdio.h gives you printing and reading tools, stdlib.h gives you system tools.
#define MAX_POKEMON 100 // Maximum Pokémon in our master list
#define MAX_TEAM 6 // Maximum Pokémon in our team (like real Pokémon games!)What this means: These are like setting rules - "You can only have 100 Pokémon total and 6 on your team, no more!"
typedef struct {
int id; // Unique number for each Pokémon
char name[30]; // Pokémon name (max 29 characters + null terminator)
char type[20]; // Type like "Fire", "Water", etc.
int hp; // Health points
int attack; // Attack power
int defense; // Defense power
char description[100]; // Description of the Pokémon
} Pokemon;Analogy: This is like creating a trading card template. Every Pokémon card has the same information slots: ID number, name, type, stats, and description.
Pokemon pokemons[MAX_POKEMON]; // Master collection (like a storage box for all cards)
Pokemon team[MAX_TEAM]; // Your active team (like cards in your deck)
int pokemonCount = 0; // How many cards are in storage
int teamCount = 0; // How many cards are in your deckAnalogy:
pokemons[] = Your big storage binder with 100 card slotsteam[] = Your battle deck with 6 card slotsThink of files like physical notebooks:
load_Pokemon_List() - Reading the Master Listvoid load_Pokemon_List() {
FILE *fp = fopen("pokemon_list.csv", "r"); // Open notebook for readingWhat's happening:
FILE *fp = Creates a "bookmark" to remember which notebook we're usingfopen("pokemon_list.csv", "r") = Opens the file named "pokemon_list.csv" in read mode if (fp == NULL) { // If notebook doesn't exist
printf(" pokemon_list.csv not found. Starting empty list.\n");
return; // Stop here and start fresh
}What this means: If the notebook doesn't exist, that's okay! We'll start with an empty collection.
while (fscanf(fp, "%d,%[^,],%[^,],%d,%d,%d,%[^\n]",
&pokemons[pokemonCount].id,
pokemons[pokemonCount].name,
pokemons[pokemonCount].type,
&pokemons[pokemonCount].hp,
&pokemons[pokemonCount].attack,
&pokemons[pokemonCount].defense,
pokemons[pokemonCount].description) == 7) {
pokemonCount++;
}The Magic of fscanf:
fscanf = "formatted scan from file" - reads data in a specific patternBreaking down the format string:
%d = "Read a number" (for ID)%[^,] = "Read everything until you hit a comma" (for name)%[^,] = "Read everything until you hit a comma" (for type)%d,%d,%d = "Read three more numbers separated by commas" (HP, attack, defense)%[^\n] = "Read everything until you hit a newline" (description)Analogy: It's like having a stamp that perfectly matches the format of each line in your notebook:
ID,Name,Type,HP,Attack,Defense,Description
25,Pikachu,Electric,35,55,40,A mouse Pokemon that stores electricityThe == 7 checks if we successfully read all 7 pieces of information. If yes, we add this Pokémon to our collection and move to the next slot.
fclose(fp); // ALWAYS close the notebook when done!
}Why close?: Like putting a notebook back on the shelf properly - prevents file corruption and frees up system resources.
save_Pokemon_List() - Writing the Master Listvoid save_Pokemon_List() {
FILE *fp = fopen("pokemon_list.csv", "w"); // Open notebook for writingKey difference: "w" means write mode - this will erase everything in the file and start fresh (like getting a new blank notebook).
for (int i = 0; i < pokemonCount; i++) {
fprintf(fp, "%d,%s,%s,%d,%d,%d,%s\n",
pokemons[i].id,
pokemons[i].name,
pokemons[i].type,
pokemons[i].hp,
pokemons[i].attack,
pokemons[i].defense,
pokemons[i].description);
}fprintf explained:
fprintf = "formatted print to file" - writes data in a specific format\n at the end = "Start a new line" (like pressing Enter)Analogy: Like using a rubber stamp to write each Pokémon card's info in your notebook, one per line, all in the same neat format.
view_Pokemon_List() - Display All Pokémonprintf("( %-3s | %-15s | %-10s | %-5s | %-6s | %-7s )\n", "ID", "Name", "Type", "HP", "ATK", "DEF");Format specifiers explained:
%-3s = "Print string, left-aligned in 3 spaces"%-15s = "Print string, left-aligned in 15 spaces"- means left-aligned (without it, text would be right-aligned)Analogy: Like creating a neat table with columns of specific widths, making everything line up perfectly.
Add_New_Pokemon() - Adding New Pokémonscanf("%29s", pokemons[pokemonCount].name);Why %29s?: The name array is size 30, but we use 29 to leave room for the null terminator (\0) that C automatically adds to end strings.
Analogy: Like leaving one blank space at the end of each line in your notebook so you know where the text ends.
scanf(" %99[^\n]", pokemons[pokemonCount].description);The space before %: Clears any leftover newline character from previous input.
%99[^\n]: Reads up to 99 characters until it hits Enter (allows spaces in description).
update_Pokemon() - Modifying Existing Pokémonfor (int i = 0; i < pokemonCount; i++) {
if (pokemons[i].id == id) { // Found the Pokémon!
// Update all fields
return; // Exit early once found
}
}Analogy: Like flipping through your trading card binder page by page until you find the specific card you want to update, then stopping once you find it.
delete_Pokemon() - Removing Pokémonfor (int j = i; j < pokemonCount - 1; j++) {
pokemons[j] = pokemons[j + 1]; // Shift everything left
}
pokemonCount--; // One less Pokémon nowWhat's happening: When you remove a card from the middle of your binder, you slide all the cards after it forward to fill the gap.
Analogy: Like removing a book from the middle of a bookshelf - you slide all the books to the right forward to close the gap.
do-while Loop Patterndo {
// Show menu
// Get user choice
// Do something based on choice
} while (choice != 6); // Keep going until user chooses to exitAnalogy: Like a restaurant that keeps asking "What would you like to order?" until you say "I'm done, bring me the check."
system("cls")This clears the screen (like pressing Ctrl+L in some terminals). It makes the interface cleaner by removing old text.
Pokemon pokemons[100] = A box with 100 numbered slots for Pokémon cardspokemonCount = A sticky note showing how many slots are filledfopen)fscanf/fprintf)fclose)Memory trick: "ORC" - Open, Read/Write, Close
if (fp == NULL) {
// Handle error
return;
}Always check if file operations worked - like checking if a door is actually open before walking through it.
Analogy: Like a Pokémon game where you manage your PC storage (full list) and your party (team of 6).
fopen = "File Open" - like opening a bookfclose = "File Close" - like closing a bookfscanf = "File Scan Formatted" - like reading with a templatefprintf = "File Print Formatted" - like writing with a templateThe 'f' at the beginning just means "file version" of the regular functions (scanf, printf, etc.).