0% found this document useful (0 votes)
12 views3 pages

Exuu 1

The document is a C program that allows users to manage student information through a file. Users can create a new file, display existing student data, or add new entries to the file. The program utilizes a menu-driven interface for user interaction and file operations.

Uploaded by

EnKo I Revenge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Exuu 1

The document is a C program that allows users to manage student information through a file. Users can create a new file, display existing student data, or add new entries to the file. The program utilizes a menu-driven interface for user interaction and file operations.

Uploaded by

EnKo I Revenge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Online C compiler to run C program online

#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:

/*Lobby to select if you want to write/display/add */


printf("1.Write(creates file) (Removes Old File Information)\n");
printf("2.Display File Information (doesn't create file) \n");
printf("3.Add (doesn't create file)\n");
printf("4.Exit :(\n");
printf("Select An Option :"); scanf("%d",&ch);

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;

case 2:goto display; break;

case 3:goto add; break;

case 4:return 0; break;

default:printf("\nchoose a valid number >:( \n \n"); ch =0 ; goto lobby;

}
/*----------------End Of LOBBY--------------*/
/*------------------------------------------*/
write: //writing and creating the file

f = fopen("D:\\etudiantMiS2.txt","w");

printf("\ngive the number of students : ");scanf("%d",&n); //Ask for the number


of students

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);

/*- Write into File -*/


fprintf(f,"%d.\n\t student's number: %d",i+1,stdnt[i].stdn);
fprintf(f,"\n\t student's FirstName: %s",stdnt[i].fname);
fprintf(f,"\n\t student's LastName: %s",stdnt[i].lname);
fprintf(f,"\n\t student's Average Grade: %f \n\n",stdnt[i].gavg);
}
fclose(f);
goto lobby;//takes you to the choice list at first

/*2. Reading and displaying what's in the file UwU */


display:
f =fopen("D:\\etudiantMiS2.txt","r");
if(f == NULL){ //in case maykonch kayan fichier maytertegch al pc
printf("\nThere is no file to display :p");
printf("\nLOOK FOR THE FILE OR USE OPTION 1 (returning to lobby)\n\n");
goto lobby;
}

char disp[1000];

while(fgets(disp,1000,f)){
printf(disp);
}

fclose(f); //close the file and go to lobby


system("pause");
goto lobby;

/* 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)

/* -- Add Part --*/ //relevant to the exo


f=fopen("D:\\etudiantMiS2.txt","a");
printf("How many students do you want to add? :"); scanf("%d",&n);
n=n+i;
for(i=i;i<n;i++){ //you could do (i=0 , i<n)/but for me as i said i wanted to
print "i"

//Same thing as Write function ( •_•)


/* - 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);

/*- Write into File -*/


fprintf(f,"%d.\n\t student's number: %d",i+1,stdnt[i].stdn);
fprintf(f,"\n\t student's FirstName: %s",stdnt[i].fname);
fprintf(f,"\n\t student's LastName: %s",stdnt[i].lname);
fprintf(f,"\n\t student's Average Grade: %f \n\n",stdnt[i].gavg);
}
fclose(f);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy