Exuu 1
Exuu 1
#include <stdio.h>
//structure
typedef struct{
int stdn;
char fname[99],lname[99];
float gavg;
}student;
/*-------------------------*/
int main() {
int i,n,ch;
char c;
student stdnt[99];
FILE *f;
lobby:
switch(ch){
case 1: printf("Confirm?(Y/N);");scanf(" %c",&c);
if(c=='Y' || c=='y'){
goto write;
}else{
ch=0; //so it doesn't loop while going back to the lobby
goto lobby;
} break;
}
/*----------------End Of LOBBY--------------*/
/*------------------------------------------*/
write: //writing and creating the file
f = fopen("D:\\etudiantMiS2.txt","w");
for(i=0;i<n;i++){
/* - Filling info - */
printf("\ngive the student's number: ");scanf("%d",&stdnt[i].stdn);
printf("\ngive the student's Firstname: ");scanf("%s",&stdnt[i].fname);
printf("\ngive the student's Lastname: ");scanf("%s",&stdnt[i].lname);
printf("\ngive the student's Average Grade: ");scanf("%f",&stdnt[i].gavg);
char disp[1000];
while(fgets(disp,1000,f)){
printf(disp);
}
/* ADD Function */
add:
if(f == NULL){ //in case maykonch kayan fichier maytertegch al pc
printf("\nThere is no file to add to :p");
printf("\nLOOK FOR THE FILE OR USE OPTION 1 (returning to lobby)\n\n");
goto lobby;
}
/*Hna Ra7 netfelsef bash nkheredj last index ("i" tae for loop fel write)*/
/*This Part is avoidable by not printing student index in the file ;)*/
i=0;
char line[256];
f=fopen("D:\\etudiantMiS2.txt","r");
while(fgets(line,sizeof(line),f)!= NULL){
//hna yeddi chaque sterr fih ""."" (wa9il)
if(sscanf(line,"%d.",&i) ==1){
//w hna chatgpt khellah fargh 9allk belli bla matche9i ro7k psq rak
t7ws 3la last index (sscanf teddi mel sterr li fih ""."" (fel cas taei ha ykon ya
i. (i howa al last index li rani n7ws 3lih) )) [%d. teddi al first integer w
tskippi al ba9i (fhad al cas teddi ghir al last index)]
}
}
fclose(f);//close the read file after getting what we need (i)
return 0;
}