Guys pahelp naman po paano po gumawa ng File delete and file update Mula po dito code ko, if pwede pacomment nalang din po Yung code, tia po! Baguhan palang kasi ako.
C++:
void creatf();
void dispf();
void searchf();
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<io.h>
#include<string.h>
typedef struct info
{
char id[5];
char name[30];
float gpa;
}info;
info record[100];
int fp, amode, permission;
int v;
char av;
char d_gpa[5];
char find_id[5];
char sel;
main()
{
do
{
clrscr();
printf("MAIN MENU \n\n");
printf("1. Create File \n");
printf("2. Display File \n");
printf("3. Search File \n");
printf("4. EXIT \n\n");
printf("Please select: ");
sel=getche();
switch(sel)
{
case '1':
clrscr();
creatf();
getche();
break;
case '2':
clrscr();
dispf();
getche();
break;
case '3':
clrscr();
searchf();
getche();
break;
case '4':
clrscr();
getche();
break;
}
}while(sel!='4');
getche();
}
//------- begin -------
void creatf()
{
clrscr();
amode=O_RDWR|O_CREAT|O_TRUNC|O_TEXT;
permission=S_IREAD|S_IWRITE;
fp=open("MASTER.TXT", amode, permission);
if(fp!=NULL)
{
v=0;
do
{
clrscr();
printf("Enter ID: ");
gets(record[v].id);
fflush(stdin);
printf("Enter Name: ");
gets(record[v].name);
fflush(stdin);
printf("Enter GPA: ");
fgets(d_gpa, sizeof(d_gpa), stdin);
record[v].gpa=atof(d_gpa);
printf("Do you want to continue? [Y/N]: ");
av=getche();
fflush(stdin);
write(fp, &record, sizeof(record));
v++;
}while((av!='N')&&(av!='n'));
}
else
{
printf("Can't open file");
getche();
}
close(fp);
getche();
}
//------- end -------
//------- begin -------
void dispf()
{
v=0;
clrscr();
gotoxy(27,1);printf("Displaying the records \n\n");
gotoxy(1,4); printf("ID");
gotoxy(11,4); printf("Name");
gotoxy(41,4); printf("GPA");
fp=open("MASTER.TXT",O_RDONLY|O_TEXT);
while(!eof(fp))
{
read(fp,&record,sizeof(record));
gotoxy(1,5+v); printf("%s", record[v].id);
gotoxy(11,5+v); printf("%s", record[v].name);
sprintf(d_gpa,"%.2f",record[v].gpa);
gotoxy(41,5+v); printf("%s", d_gpa);
v++;
}
gotoxy(27,20); printf("Nothing follows!!!");
close(fp);
getche();
}
//------- end -------
//------- begin -------
void searchf()
{
v=0;
clrscr();
printf("Search From The Master File \n\n");
printf("Type ID to search: ");
gets(find_id);
amode= O_RDONLY|O_TEXT;
fp=open("MASTER.TXT", amode);
while(!eof(fp))
{
read(fp, &record, sizeof(record));
if(((strcmp(find_id, record[v].id))==0))
{
clrscr();
gotoxy(6,5); printf("ID");
gotoxy(16,5); printf("Name");
gotoxy(42,5); printf("GPA");
gotoxy(6,7); printf("%s", record[v].id);
gotoxy(16,7); printf("%s", record[v].name);
sprintf(d_gpa, "%.2f", record[v].gpa);
gotoxy(42,7); printf("%s", d_gpa);
break;
}
else
{
clrscr();
printf("No Records Found");
}
v++;
}
close(fp);
getche();
}
//------- end -------