#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char Name[50];
char FN[7];
int marks[3];
float SRU;
};
char fileName[31];
FILE *fs;
Student st;
void createFile(){ //1
fs=fopen(fileName,"wb");
fclose(fs);
}
void ReadStudent (Student *st) {
int i;
printf ("Name: "); scanf("%s",st->Name); getchar();
printf ("Faculty number: "); scanf("%s",st->FN);
printf ("Insert the marks of Physics(1), Mathematics(2) and Programming (3): \n");
for (i=0;i<3;i++) {
printf ("Subject %d-a: ",i+1);
scanf ("%d",&st->marks[i]);
}
}
void addStudents(){ //2
fs=fopen(fileName, "a+b");
char c;
do {
ReadStudent(&st);
fwrite(&st, sizeof(Student),1,fs);
printf("More (y/n)?"); c=getchar(); getchar();
}while(c=='y');c=getchar(); getchar();
fclose(fs);
}
void changeData(){ // 3
char j[11];
int k=0;
// if(!exist(fileName)){
// printf("The file's not created\n\n"); return;
printf("Insert the faculty number of the student whose data you'd like you change "); gets(j);
fs=fopen (fileName, "r+b");
fread(&st,sizeof(Student),1,fs);
while (!feof(fs)) {
if(strcmp(st.FN,j)==0) {
ReadStudent(&st);
fseek(fs,-(long)sizeof(Student), SEEK_CUR);
fwrite(&st, sizeof(Student),1,fs);
printf("The data has been changed\n");
k++;
break;
}
fread(&st, sizeof(Student),1,fs);
}if (k==0) printf("No data for the student with that FN\n");
fclose(fs);
}
void arithmAv()
{
fs=fopen(fileName,"rb");
FILE *txt=fopen("Students.txt","wt");
int count=0;
//Here I should write something but idk what...
fprintf(txt, "There are %d student(s)\n",count);
fclose(fs);
fclose(txt);
}
void main() {
system("chcp 1251");
printf("File name: ");
gets(fileName);
int answer;
do{
printf("\n\nMenu:\n");
printf("1-Create an empty file\n");
printf("2-Add data for a student to the file\n");
printf("3-Change data for a student by FN\n");
printf("4-Arithmetic average of the 3 subjects seperately and output in text file.\n");
printf("0-Exit\n");
char c;
printf("Choose: "); scanf("%d", &answer);c=getchar(); getchar();
switch (answer){
case 1:createFile(); break;
case 2:addStudents(); break;
case 3:changeData(); break;
case 4:arithmAv(); break;
}
}while(answer !=0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char Name[50];
char FN[7];
int marks[3];
float SRU;
};
char fileName[31];
FILE *fs;
Student st;
void createFile(){ //1
fs=fopen(fileName,"wb");
fclose(fs);
}
void ReadStudent (Student *st) {
int i;
printf ("Name: "); scanf("%s",st->Name); getchar();
printf ("Faculty number: "); scanf("%s",st->FN);
printf ("Insert the marks of Physics(1), Mathematics(2) and Programming (3): \n");
for (i=0;i<3;i++) {
printf ("Subject %d-a: ",i+1);
scanf ("%d",&st->marks[i]);
}
}
void addStudents(){ //2
fs=fopen(fileName, "a+b");
char c;
do {
ReadStudent(&st);
fwrite(&st, sizeof(Student),1,fs);
printf("More (y/n)?"); c=getchar(); getchar();
}while(c=='y');c=getchar(); getchar();
fclose(fs);
}
void changeData(){ // 3
char j[11];
int k=0;
// if(!exist(fileName)){
// printf("The file's not created\n\n"); return;
printf("Insert the faculty number of the student whose data you'd like you change "); gets(j);
fs=fopen (fileName, "r+b");
fread(&st,sizeof(Student),1,fs);
while (!feof(fs)) {
if(strcmp(st.FN,j)==0) {
ReadStudent(&st);
fseek(fs,-(long)sizeof(Student), SEEK_CUR);
fwrite(&st, sizeof(Student),1,fs);
printf("The data has been changed\n");
k++;
break;
}
fread(&st, sizeof(Student),1,fs);
}if (k==0) printf("No data for the student with that FN\n");
fclose(fs);
}
void arithmAv()
{
fs=fopen(fileName,"rb");
FILE *txt=fopen("Students.txt","wt");
int count=0;
//Here I should write something but idk what...
fprintf(txt, "There are %d student(s)\n",count);
fclose(fs);
fclose(txt);
}
void main() {
system("chcp 1251");
printf("File name: ");
gets(fileName);
int answer;
do{
printf("\n\nMenu:\n");
printf("1-Create an empty file\n");
printf("2-Add data for a student to the file\n");
printf("3-Change data for a student by FN\n");
printf("4-Arithmetic average of the 3 subjects seperately and output in text file.\n");
printf("0-Exit\n");
char c;
printf("Choose: "); scanf("%d", &answer);c=getchar(); getchar();
switch (answer){
case 1:createFile(); break;
case 2:addStudents(); break;
case 3:changeData(); break;
case 4:arithmAv(); break;
}
}while(answer !=0);
}
To copy to clipboard, switch view to plain text mode
Bookmarks