C언어 셀프 학습

셀프학습2

yhat 2014. 7. 2. 22:17

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<Windows.h>


struct stud{

char name[15];
int kor;
int eng;
int math;
int tot;
float avg;
};
void input(void);
void output(void);


main()
{
int i;
for(i=0;i<=2;i++)
  {
input();
  }

 

output();

}

void input(void)
{


struct stud *asd;
 FILE *fp;

fp=fopen("sung.dat","ab");

asd=(struct stud*)calloc(sizeof(struct stud),1);
 


printf("이름\n");
scanf("%s",&(*asd).name);

printf("국어\n");
scanf("%d",&(*asd).kor);

printf("영어\n");
scanf("%d",&(*asd).eng);

printf("수학\n");
scanf("%d",&(*asd).math);

(*asd).tot = (*asd).kor + (*asd).eng + (*asd).math;

(*asd).avg = (*asd).tot/3;

 

fwrite(asd,sizeof(struct stud),1,fp);

fclose(fp);
 

free(asd);
}
void output(void)
{
  int i;
struct stud ask;
FILE *dp;

  dp=fopen("sung.dat","rb");
 

  fread(&ask,sizeof(struct stud),1,dp);

{


    for(i=0;i<=2;i++)
    {

        //fseek(fp, i*sizeof(struct stud), SEEK_SET);
         printf("%15s%10d%10d%10d%15d%15f\n",ask.name,(ask).kor,(ask).eng,(ask).math,(ask).tot,(ask).avg);
         fread(&ask,sizeof(struct stud),1,dp);
        if (feof(dp)) break;
    }
    fclose(dp);
}
}