#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
FILE *data,*tmp;
int reg_std();
int mod_data();
struct student
{
char name[30];
char prog[15];
char ID[15];
char intake[10];
};
int main()
{
int i=1;
while(i)
{
reg_std();
printf("Enter 0 to stop and 1 to continue\n");
scanf("%d",&i);
}
mod_data();
return 0;
}
int reg_std()
{
struct student s;
char intc[2];
int flag=0;
strcpy(s.intake,"Intake ");
data=fopen("data.txt","a+");
if(!data)
{
printf("Data File can not be opened\n");
}
else
{
printf("Enter The Name of Student: ");
fflush(stdin);
gets(s.name);
fprintf(data,"%-20.30s",s.name);
printf("Enter Student`s ID No: ");
fflush(stdin);
gets(s.ID);
fprintf(data,"%-15.15s",s.ID);
printf("Enter Intake No: ");
fflush(stdin);
gets(intc);
strcat(s.intake,intc);
fprintf(data,"%-10.10s",s.intake);
printf("Enter Program Name: ");
fflush(stdin);
gets(s.prog);
fprintf(data,"%-10.15s\n",s.prog);
fclose(data);
flag=1;
}
if(flag)
return 0;
else
return -1;
}
int mod_data()
{
struct student mod_d;
char id[15];
char intc[2];
printf("Enter student`s ID No whose record has to be modified\n");
scanf("%s",id);
data=fopen("data.txt","r");
if(data==NULL)
{
printf("Data file can not be opened\n");
return 0;
}
tmp=fopen("tmp.txt","w");
if(tmp==NULL)
{
printf("Temporary file can not be created\n");
return 0;
}
while(!feof(data))
{
fscanf(data,"%s %s %s %s",mod_d.name,mod_d.ID,mod_d.intake,mod_d.prog);
printf("%s\t%s\t%s\t%s\n",mod_d.name,mod_d.ID,mod_d.intake,mod_d.prog);
if(strcmp(id,mod_d.ID)==0)
{
printf("Enter modified name of the student: ");
fflush(stdin);
gets(mod_d.name);
fputs(mod_d.name,tmp);
fputs(mod_d.ID,tmp);
printf("Enter modified intake no: ");
fflush(stdin);
gets(intc);
strcpy(mod_d.intake,"Intake ");
strcat(mod_d.intake,intc);
fputs(mod_d.intake,tmp);
printf("Enter modified program name: ");
gets(mod_d.prog);
fputs(mod_d.prog,tmp);
}
else
{
fputs(mod_d.name,tmp);
fputs(mod_d.ID,tmp);
fputs(mod_d.intake,tmp);
fputs(mod_d.prog,tmp);
}
}
fclose(data);
fclose(tmp);
return 0;
}
asked
10 Jan '16, 12:16
Anisur Rahman Tonu
207●14