Code:
/*********** Insert First ***********/
void insert_fist(FILE *f){
FILE *f2;
data R;
f=fopen("f.dat","r");
f2=fopen("f2.dat","w");
read(&R);
fprintf(f2,"%d %f ",R.id,R.gpa);
while(!feof(f)){
fscanf(f,"%d %f ",&R.id,&R.gpa);
fprintf(f2,"%d %f ",R.id,R.gpa);
}
fclose(f);
fclose(f2);
f=fopen("f.dat","w");
f2=fopen("f2.dat","r");
while(!feof(f2)){
fscanf(f2,"%d %f ",&R.id,&R.gpa);
fprintf(f,"%d %f ",R.id,R.gpa);
}
fclose(f);
fclose(f2);
}
------
Code:
#include<stdio.h>
/*********** Data ***********/
typedef struct {
int id;
float gpa;
}data;
/*********** The functions ***********/
void read(data*);
void print(FILE*);
void insert_last(FILE*);
void insert_first(FILE*);
void insert_After_Target(FILE*,int);
void delet_first(FILE*);
int search(FILE*,int);
int size(FILE*);
void copy(FILE*,FILE*);
void menu();
/*********** Main ***********/
int main(){
data R;
FILE *f;
int choice;
int i,t,n;
if((f=fopen("f.dat","w"))==NULL)
printf("sorry i can not open the file\n");
else{
for(i=0;i<3;i++){
read(&R);
fprintf(f,"%d %.2f ",R.id,R.gpa);
}
fclose(f);
menu();
scanf("%d",&choice);
while(choice!=6){
switch(choice){
case 1:
insert_first(f);
menu();
break;
case 2:
insert_last(f);
menu();
break;
case 3:
printf("Enter the Taget: ");
scanf("%d",&t);
n=search(f,t);
printf("n=%d\n",n);
if(n==1)
insert_After_Target(f,t);
else
printf("Target not found\n");
menu();
break;
case 4:
print(f);
menu();
break;
case 5:
delet_first(f);
menu();
default :
printf("Error choice try agan\n");
menu();
break;
}
scanf("%d",&choice);
}
}
return 0;
}
/*********** Menu ***********/
void menu(){
printf("+------------------------------------------------------+");
printf("\n| | The Main Menu | |");
printf("\n+------------------------------------------------------+");
printf("\n| 2- Inserts a new data in the first. |");
printf("\n| 2- Inserts a new data in the last. |");
printf("\n| 3- Inserts After Target. |");
printf("\n| 4- Print The File. |");
printf("\n| 5- Delet First. |");
printf("\n| 6- End. |");
printf("\n+------------------------------------------------------+");
printf("\nEnter your choice :");
}
/*********** Read ***********/
void read(data *R){
printf("Enter the id :");
scanf("%d",&R->id);
printf("\n");
printf("Enter the gpa :");
scanf("%f",&R->gpa);
printf("\n");
}
/*********** Print ***********/
void print(FILE *f){
data R;
if((f=fopen("f.dat","r"))==NULL)
printf("sorry i can not open the file\n");
else{
fscanf(f,"%d %f",&R.id,&R.gpa);
while(!feof(f)){
printf("The id is: %d the gpa is: %.2f\n",R.id,R.gpa);
fscanf(f,"%d %f",&R.id,&R.gpa);
}
fclose(f);
}
}
/*********** Insert Last ***********/
void insert_last(FILE *f){
data R;
if((f=fopen("f.dat","a"))==NULL)
printf("Sorry can not open the file\n");
else{
read(&R);
fprintf(f,"%d %f ",R.id,R.gpa);
fclose(f);
}
}
/*********** Insert After Target ***********/
void insert_After_Target(FILE *f,int t){
FILE *f2;
data R;
data Z;
f=fopen("f.dat","r");
f2=fopen("f2.dat","w");
while(!feof(f)){
fscanf(f,"%d %f ",&R.id,&R.gpa);
fprintf(f2,"%d %f ",R.id,R.gpa);
if(R.id==t){
read(&Z);
fprintf(f2,"%d %f ",Z.id,Z.gpa);
}
}
fclose(f);
fclose(f2);
f=fopen("f.dat","w");
f2=fopen("f2.dat","r");
while(!feof(f2)){
fscanf(f2,"%d %f ",&R.id,&R.gpa);
fprintf(f,"%d %f ",R.id,R.gpa);
}
fclose(f);
fclose(f2);
}
/*********** Search ***********/
int search(FILE *f,int t){
data R;
int m=0;
f=fopen("f.dat","r");
while(!feof(f)){
fscanf(f,"%d %f ",&R.id,&R.gpa);
if(R.id==t){
m=1;
fclose(f);
return m;
}
}
fclose(f);
return m=0;
}
/*********** Size ***********/
int size(FILE*f){
int c=0;
data R;
f=fopen("f.dat","r");
while(!feof(f)){
fscanf(f,"%d %f ",&R.id,&R.gpa);
c++;
}
fclose(f);
return c;
}
/*********** Delet First ***********/
void delet_first(FILE *f){
FILE *f2;
data R;
f=fopen("f.dat","r");
f2=fopen("f2.dat","w");
fscanf(f,"%d %f ",&R.id,&R.gpa);
while(!feof(f)){
fscanf(f,"%d %f ",&R.id,&R.gpa);
fprintf(f2,"%d %f ",R.id,R.gpa);
}
fclose(f);
fclose(f2);
f=fopen("f.dat","w");
f2=fopen("f2.dat","r");
while(!feof(f2)){
fscanf(f2,"%d %f ",&R.id,&R.gpa);
fprintf(f,"%d %f ",R.id,R.gpa);
}
fclose(f);
fclose(f2);
}
/*********** Copy ***********/
void copy(FILE *f,FILE *f2){
data R;
while(!feof(f2)){
fscanf(f2,"%d %f ",&R.id,&R.gpa);
fprintf(f,"%d %f ",R.id,R.gpa);
}
}
/*********** Insert First ***********/
void insert_fist(FILE *f){
FILE *f2;
data R;
f=fopen("f.dat","r");
f2=fopen("f2.dat","w");
read(&R);
fprintf(f2,"%d %f ",R.id,R.gpa);
while(!feof(f)){
fscanf(f,"%d %f ",&R.id,&R.gpa);
fprintf(f2,"%d %f ",R.id,R.gpa);
}
fclose(f);
fclose(f2);
f=fopen("f.dat","w");
f2=fopen("f2.dat","r");
while(!feof(f2)){
fscanf(f2,"%d %f ",&R.id,&R.gpa);
fprintf(f,"%d %f ",R.id,R.gpa);
}
fclose(f);
fclose(f2);
}
/*********** End ***********/