Hi everyone!!
i have a problem here. i am writing a database program in C in which i used files and structures to store data. but my code is not running :/
here is my code::
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct std_info
{
char name[15];
char seatno[9];
float ssc;
float hsc;
int ccy;
};
struct std_info si;
static char *numstr[9];
int n = 0;
FILE *fp;
void menu();
void form();
void writeData();
void readNext(int pos);
void readPrev(int pos);
int key_chk();
int main()
{
int key,key1;
int nextPos =1;
int PrevPos;
system("cls");
menu();
gotoxy(20,6);
// i am facing problem i this while loop nothing happend
//when i used switch it works for writing data to the file and showing next recode in the file but not previouse
while((key = key_chk()) != 24)
{
if(key == 1)
{
do
{
system("cls");
menu();
writeData();
printf("\n\nAdd more data y/n? : ");
}while(getchar() == 'y');
}
if(key == 3) // checks if user want to see data
{
key1 = key_chk();
if(key1 == 77) //if user press right arrow key display next data in a file
{
system("cls");
menu();
readNext(nextPos);
nextPos++;
PrevPos = nextPos;
}
if(key1 == 75)//if user press left arrow key go back to previous data in a file
{
system("cls");
menu();
readPrev(PrevPos);
PrevPos--;
}
nextPos = PrevPos;
}
}
getch();
return 0;
}
void menu() // menu
{
static char *items[4] = {"ADD","UPDATE","VIEW","EXIT"};
gotoxy(20,5);
textcolor(0);
textbackground(15);
for(int i=0; i<4; i++)
{
printf(" %d:",i+1);
cprintf(" %s ",*(items+i));
}
}
void form() //form
{
gotoxy(20,8);
textcolor(15);
textbackground(0);
cprintf("Seat No. : ");
gotoxy(20,9);
cprintf("Student's Name : ");
gotoxy(20,10);
cprintf("SSC Percentage : ");
gotoxy(20,11);
cprintf("HSC Percentage : ");
gotoxy(20,12);
cprintf("Course Cleared : ");
}
void writeData() writing data into the file
{
if((fp = fopen("std_info.DAT","ab+")) == NULL)
{
gotoxy(20,30);
cprintf("Can't open file");
exit(1);
}
//system("cls");
form();
gotoxy(40,8);
gets(si.name);
gotoxy(40,9);
gets(si.seatno);
gotoxy(40,10);
gets(*numstr);
si.ssc = atof(*numstr);
gotoxy(40,11);
gets(*numstr);
si.hsc = atof(*numstr);
gotoxy(40,12);
gets(*numstr);
si.ccy = atoi(*numstr);
fwrite(&si,sizeof(si),1,fp);
fflush(stdin);
fclose(fp);
}
void readNext(int pos) reading data
{
if((fp=fopen("std_info.DAT","rb")) == NULL)
{
gotoxy(20,30);
cprintf("Can't open file");
exit(1);
}
long offset;
offset = (pos-1) * sizeof(si);
form();
textcolor(15);
textbackground(0);
fseek(fp,offset,SEEK_SET);
fread(&si,sizeof(si),1,fp);
gotoxy(40,8);
cprintf("%s",si.seatno);
gotoxy(40,9);
cprintf("%s",si.name);
gotoxy(40,10);
cprintf("%.1f",si.ssc);
gotoxy(40,11);
cprintf("%.1f",si.hsc);
gotoxy(40,12);
cprintf("%d",si.ccy);
fflush(stdin);
fclose(fp);
}
void readPrev(int pos) //reading data
{
if((fp=fopen("std_info.DAT","rb")) == NULL)
{
gotoxy(20,30);
cprintf("Can't open file");
exit(1);
}
long offset;
offset = (pos-1) * sizeof(si);
form();
textcolor(15);
textbackground(0);
fseek(fp,offset-1,SEEK_CUR);
fread(&si,sizeof(si),1,fp);
gotoxy(40,8);
cprintf("%s",si.seatno);
gotoxy(40,9);
cprintf("%s",si.name);
gotoxy(40,10);
cprintf("%.1f",si.ssc);
gotoxy(40,11);
cprintf("%.1f",si.hsc);
gotoxy(40,12);
cprintf("%d",si.ccy);
fflush(stdin);
fclose(fp);
}
int key_chk() // this the code for checking which key is pressed (extended or normal key)
{
char ch,ch1;
ch = getch();
if(ch == 0)
{
ch1 = getch();
}
else{return ch;}
return ch1;
}
any help would be appreciated.