C 언어/임베디드 C
구조체를 가리키는 포인트변수
알 수 없는 사용자
2011. 7. 7. 17:30
//데이터 파일에서 몸무게와 키를 읽어 비만을 체크하는 프로그램
#include <stdio.h>
#include <math.h> //sin() 함수 사용
#include <process.h> //exit() 함수
typedef struct
{
char name[20];
float height;
float weight;
}STUDENT;
int main()
{
FILE *fp;
STUDENT st, *sp; //구조체 변수
int i = 0;
if((fp = fopen("d9-5.dat", "r")) == NULL)
{
printf("File Open Error!\n");
exit(-1);
}
sp = &st; //sp이 구조체 변수 st의 주소를 갖는다.
while(!feof (fp)) //이건 옛날 방식이고 while(feof == 0)으로 표시한다.
{
fscanf(fp, "%s %f %f", sp->name, &sp->height, &sp->weight);
printf("student name : %s\n", sp->name); //(*sp).name
printf("Height : %3.1f\nWeight : %3.1f\n", sp->height, sp->weight);
if((sp->height - 109) + 10 <= sp->weight)
{
printf("You are fat.. You need diet!!!\n\n");
}
else if((sp->height - 109) - 10 >= sp->weight)
{
printf("You are thin.. You need more food!!!\n\n");
}
else
{
printf("You are normal... Keep your shape!!!\n\n");
}
}
fclose(fp);
return 0;
}
입력파일
jaesung 202.2 63.1
jungwoo 177.7 66.6
jaehun 178.2 100.1
jaesung 202.2 63.1
jungwoo 177.7 66.6
jaehun 178.2 100.1