Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void writeNamesToFile(char []);
- void writerollToFile(char []);
- void saveDataToStructure();
- void takeInputs();
- void showAll();
- FILE *fp;
- int strucsize=0;
- int x,i;
- struct student
- {
- char names[100];
- int roll;
- char dept;
- double cgpa;
- } info[100];
- int main()
- {
- ///file theke data niye structure a save korbe...program er surutei ei kaj korbe
- saveDataToStructure();
- ///
- ///choice nibe j user ki korte chaay...eikhane search aro habi jabi add korba
- while(1)
- {
- printf("\n\n\nWhat do you want to do?\n");
- printf("1. Add Data");
- printf("\n2.Show Data");
- printf("\n3.Search info By roll\n");
- int choice;
- scanf("%d",&choice);
- if(choice==1)
- {
- fflush(stdin); ///gtoklker porblem taa....jeno gets ekta skip naa kore
- takeInputs();
- saveDataToStructure();///input niye toh file a rakhlam...oita abr structure a load kore rakhbo
- }
- else if(choice==2)
- {
- showAll();
- }
- else if(choice==3)
- {
- search();
- }
- }
- }
- ///structure er sob element show korbe...bujhar e kotha
- void showAll()
- {
- int i=0;
- for(i=0; i<strucsize; i++)
- {
- printf("-----%d-----",i+1);
- printf("\nStudent name: %s",info[i].names);
- printf("Number: %d",info[i].roll);
- printf("\n\n");
- }
- }
- ///input niye oita file a save korbe
- void takeInputs()
- {
- printf("Type exit to stop taking data\n");
- char names[100];
- char num[100];
- while(1)
- {
- printf("Enter names: ");
- gets(names);
- if(strcmp(names,"exit")==0)
- break;
- else
- {
- printf("Enter roll: ");
- gets(num);
- if(strcmp(names,"exit")==0) ///same check jodi exit likhe r input nibe na
- {
- break;
- }
- else
- {
- ///prottek taar sathe ekta newline add kore save korbo file a
- writeNamesToFile(strcat(names,"\n"));
- writerollToFile(strcat(num,"\n"));
- }
- }
- }
- }
- void writeNamesToFile(char str[])
- {
- fp=fopen("names.txt","a+");
- fprintf(fp,str);
- fclose(fp);
- }
- void writerollToFile(char str[])
- {
- fp=fopen("roll.txt","a+");
- fprintf(fp,str);
- fclose(fp);
- }
- void writedepttofiles(char str[])
- {
- fp=fopen("dept.text","a+");
- fprintf(fp,str);
- fclose(fp);
- }
- void writeCGPAtofiles(char str[])
- {
- fp=fopen("cgpa","a+");
- fprintf(fp,str);
- fclose(fp);
- }
- void saveDataToStructure()
- {
- FILE *fproll,*fpnames,*fpdept,*fpcgpa;
- fproll=fopen("roll.txt","r+");
- fpnames=fopen("names.txt","r+");
- fpdept=fopen("dept.txt","r+");
- fpcgpa=fopen("cgpa","r+");
- strucsize=0;
- char ch;
- char numstr[10][100];
- int i=0,j=0;
- ///roll save
- while((ch=fgetc(fproll))!=EOF)
- {
- numstr[i][j]=ch;
- j++;
- if(ch=='\n')
- {
- info[i].roll=atoi(numstr[i]);
- i++;
- j=0;
- strucsize++;
- }
- }
- i=0;
- j=0;
- ///name save
- while((ch=fgetc(fpnames))!=EOF)
- {
- info[i].names[j]=ch;
- if(ch=='\n')
- {
- i++;
- j=0;
- }
- }
- i=0;
- j=0;
- /// dept save
- while((ch=fgetc(fpdept))!=EOF)
- {
- info[i].dept[j]=ch;
- j++;
- if(ch=='\n')
- {
- i++;
- j=0;
- }
- }
- ///cg save
- char cgstr[10][100];
- while((ch=fgetc(fpcgpa))!=EOF)
- {
- cgstr[i][j]=ch;
- j++;
- if(ch=='\n')
- {
- info[i].roll=atoi(cgstr[i]);
- i++;
- j=0;
- strucsize++;
- }
- }
- fclose(fproll);
- fclose(fpnames);
- fclose(fpdept);
- fclose(fpcgpa);
- }
- void search(char roll)
- {
- printf("Enter roll to search: \n");
- scanf("%d",&x);
- for(i=0;i<strucsize;i++)
- {
- if(x==info[i].roll)
- {
- printf("%s\t %d\n%s\t%lf",info[i].names,info[i].roll,info[i].dept,info[i].cgpa);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement