/*
This program is used to input file, search particular record and display all the record. I'm using TcLite to compile.
*/
/*PROBLEM...
1) The data type i used to search for a particular record is char, everytime i do a searching, it cannot found any record. I try to change the date type to int, then the program working correctly.
2) My Display function is also not working.
Can some one compile my program and help to solve my problem?
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define false 0
#define true 1
void add();
void display();
void search();
FILE *MyFile;
int bytesWritten;
int bytesRead;
char found;
char wanted[10]; /*If i using int, the program is working correctly. */
struct WizardStructure
{
char Name[30], Magic[100];
char WizardID[10]; /*If using int, program working correctly*/
}Wizard;
///////////////////////////////////////////////////////////////////////////////////
int main()
{
char i, choice;
do{
clrscr();
printf("\n Main Menu");
printf("\n 1. Add new Wizard");
printf("\n 2. Display all Wizard's name and ID");
printf("\n 3. Search Wizard record");
printf("\n 4. Exit");
printf("\n\n Please enter your choice : ");
scanf(" %[^\n]", &choice);
switch (choice)
{
case '1' : add(); break;
case '2' : display(); break;
case '3' : search(); break;
default: printf("\n Exit the program ");
}
printf("\n Do you want to continue? Press [ Y ] for YES... else NO : ");
scanf(" %c", &i);
}while(i=='y'|| i=='Y');
getch();
return 0;
}
/* To add record *************************************/
void add()
{
MyFile = fopen("Wizard.dat", "r+b");
if (!MyFile)
MyFile = fopen("Wizard.dat", "w+b");
printf("\n Enter Wizard name : ");
scanf(" %[^\n]", &Wizard.Name);
printf("\n Enter Wizard ID : ");
scanf(" %[^\n]", &Wizard.WizardID); /*if using %d*/
printf("\n Enter MAGIC POWER of the Wizard : ");
scanf(" %[^\n]", &Wizard.Magic);
fseek (MyFile, 0, SEEK_END);
bytesWritten = fwrite((void*)&Wizard, sizeof(Wizard), 1, MyFile);
fclose (MyFile);
}
/*To display all the records *****************************/
void display()
{
fprintf(stdout, "\n Wizard Name\t\t Wizard ID\t\t Wizard Magic");
fprintf(stdout, "\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~");
FILE *MyFile;
MyFile = fopen ("Wizard", "r+b");
if (fseek (MyFile, 0, SEEK_SET) == 0 );
{
bytesRead = fread ((void*) & Wizard, 1, sizeof(Wizard), MyFile);
while ( !feof (MyFile))
{
fprintf(stdout, "\n %s\t\t\t %s\t\t\t %s", Wizard.Name, Wizard.WizardID, Wizard.Magic);
bytesRead = fread ((void*) & Wizard, 1, sizeof(Wizard), MyFile);
}
}
}
/* To search for a particular record ***********************/
void search()
{
MyFile = fopen ("Wizard.dat", "r+b");
printf("\n Enter Wizard's ID : ");
scanf(" %[^\n]", &wanted); /*if using int*/
fseek (MyFile, 0, SEEK_SET);
found = false;
while (( !feof (MyFile)) && (found != true))
{
bytesRead = fread ((void*) & MyFile, sizeof (MyFile), 1, MyFile);
if (bytesRead > 0)
if ("Wanted" == "Wizard.WizardID")/*if without" "*/
found = true;
}
if (found == true)
{
printf("\n WizardID : %s", Wizard.WizardID);
printf("\n Name : %s", Wizard.Name);
printf("\n Wizard's Magic : %s", Wizard.Magic);
}
else
printf("\n WizardID is not found");
fclose(MyFile);
}