Last active
December 11, 2015 06:29
-
-
Save vertrigo/4559673 to your computer and use it in GitHub Desktop.
Переработанный в нормальный вид пример из методички
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <conio.h> | |
#include <string.h> | |
void main(void) | |
{ | |
struct stud | |
{ | |
char fam[10],gr[10]; | |
int year,rt[5],max; | |
}; | |
stud ek[3]={{"Иванов","ЭК-03-1",1986,{70,55,55,60,75}}, | |
{"Петров","ЭК-03-2",1986,{61,62,63,74,55}}, | |
{"Сидоров","ЭК-03-2",1986,{98,99,97,90,91}}}, s1; | |
// ищем максимальный рейтинг | |
int i,j,max; | |
for (i=0;i<3;i++) | |
{ | |
max=ek[i].rt[1]; | |
for (j=0;j<5;j++) | |
{ | |
if (ek[i].rt[j] > max) {max=ek[i].rt[j];} | |
} | |
ek[i].max=max; | |
} | |
FILE *f,*g; | |
// запись в файл | |
f=fopen("rgr2.dat","w"); | |
for (i=0;i<3;i++) | |
{ | |
fwrite(&ek[i],sizeof(stud),1,f); | |
} | |
fclose(f); | |
// чтение | |
printf("Содержимое файла:\n"); | |
if ((f=fopen("rgr2.dat","r"))==NULL) | |
{ | |
printf("Ошибка при чтении файла!\n"); | |
return; | |
} | |
g=fopen("rgr2.txt","w"); | |
fprintf(g,"Содержимое файла:\n"); | |
do { | |
fread(&s1,sizeof(stud),1,f); | |
if (!feof(f)) | |
{ | |
printf("%12s%12s%6i ",s1.fam,s1.gr,s1.year); | |
fprintf(g,"%12s%12s%6i ",s1.fam,s1.gr,s1.year); | |
for (j=0;j<5;j++) | |
{ | |
printf("%3i",s1.rt[j]); fprintf(g,"%3i",s1.rt[j]);} | |
printf("%6i\n",s1.max); fprintf(g,"%3i\n",s1.max); | |
} | |
} while (!feof(f)); | |
// чтение с поиском | |
char group[10]; | |
printf("Введите группу для поиска:\n"); | |
scanf("%s",&group); | |
printf("Студенты группы %s:\n",group); | |
fprintf(g,"Студенты группы %s:\n",group); | |
fseek(f,0L,SEEK_SET); | |
do { | |
fread(&s1,sizeof(stud),1,f); | |
if ((!feof(f))&&(!strcmp(s1.gr,group))) | |
{ | |
printf("%12s%12s%6i ",s1.fam,s1.gr,s1.year); | |
fprintf(g,"%12s%12s%6i ",s1.fam,s1.gr,s1.year); | |
for (j=0;j<5;j++) | |
{ | |
printf("%3i",s1.rt[j]); fprintf(g,"%3i",s1.rt[j]); | |
} | |
printf("%6i\n",s1.max); fprintf(g,"%3i\n",s1.max); | |
} | |
} while (!feof(f)); | |
fclose(f);fclose(g); | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment