Hello I am at a lost of how to search for data in a file which was populated through structure.
Supposed I have 15 chemicals and each of them contains additional information (e.g. Chemical Name, Chemical Formula, Chemical type, State of the chemical, Antidote name etc). If I want to search for an entry under chemical type. How should I do?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
typedef struct {
struct {
char name[30];
char formulae[8];
char type[20];
char state[20];
char pH[10];
char antidote[20];
} basic_info;
struct {
char supplier[50];
char e_contact[15];
int quantity;
char location[6];
char sub_loc[15];
char expiry[11]; //format dd/mm/yyyy
} add_info;
} database;
database chemical;
FILE *chemStore;
void Append_Database (void);
void Search_Name (void);
void Search_Suppler (void);
void Search_Type_SLoc (void);
void main()
{
char choice;
printf("=================\n");
printf("Chemical Database\n");
printf("=================\n");
printf("1. Add new chemical.\n");
printf("2. Search chemical via name.\n");
printf("3. Search chemical via supplier.\n");
printf("4. Search chemical via type & storage location.\n");
printf("q. Exit\n");
do {
printf("\nEnter Choice: ");
fflush(stdin);
choice = getchar();
choice=tolower(choice);
switch (choice)
{
case '1': Append_Database();
break;
case '2': Search_Name();
break;
case '3': Search_Suppler();
break;
case '4': Search_Type_SLoc();
break;
case 'q': printf("\nGoodbye!");
exit(0);
default: printf("Invalid choice. Please repick.\n");
break;
}
} while (choice != 'q');
}
void Append_Database()
{
printf("\nRetriving Database...\n\n");
chemStore = fopen("Chemical_Database.txt", "a");
printf("Chemical Name: ");
fflush(stdin);
gets(chemical.basic_info.name);
fprintf(chemStore, "\n%s,", chemical.basic_info.name);
printf("Formulae: ");
fflush(stdin);
gets(chemical.basic_info.formulae);
fprintf(chemStore, "%s,", chemical.basic_info.formulae);
printf("Corrosive Level: ");
fflush(stdin);
gets(chemical.basic_info.type);
fprintf(chemStore, "%s,", chemical.basic_info.type);
printf("State: ");
fflush(stdin);
gets(chemical.basic_info.state);
fprintf(chemStore, "%s,", chemical.basic_info.state);
printf("pH Level: ");
fflush(stdin);
gets(chemical.basic_info.pH);
fprintf(chemStore, "%s,", chemical.basic_info.pH);
printf("Antidote: ");
fflush(stdin);
gets(chemical.basic_info.antidote);
fprintf(chemStore, "%s,", chemical.basic_info.antidote);
printf("Supplier: ");
fflush(stdin);
gets(chemical.add_info.supplier);
fprintf(chemStore, "%s,", chemical.add_info.supplier);
printf("Emergency Contact No.: ");
fflush(stdin);
gets(chemical.add_info.e_contact);
fprintf(chemStore, "%s,", chemical.add_info.e_contact);
printf("Quantity: ");
fflush(stdin);
scanf("%d", &chemical.add_info.quantity);
fprintf(chemStore, "%d,", chemical.add_info.quantity);
printf("Location: ");
fflush(stdin);
gets(chemical.add_info.location);
fprintf(chemStore, "%s,", chemical.add_info.location);
printf("Sub Location: ");
fflush(stdin);
gets(chemical.add_info.sub_loc);
fprintf(chemStore, "%s,", chemical.add_info.sub_loc);
printf("Expiry Datte: ");
fflush(stdin);
gets(chemical.add_info.expiry);
fprintf(chemStore, "%s,", chemical.add_info.expiry);
fclose(chemStore);
}
Theportion above will write into Chemical_Database txt file as attached. Every field is separated by comma (.csv format).
However I am lost at how to implement search functions to search through the 'database'.
I am thinking that I have to somehow read every line in the txt file and store in a array. But how should I go about doing it with structures?
Lastly, how to search through the array?