#define MAX_GAKUSEKI 10 #define MAX_NAME 20 typedef struct{ char gakuseki[MAX_GAKUSEKI]; char name[MAX_NAME]; } Student;
int set_gakuseki(Student *s,const char in[]) { int i; for( i=0 ; i < MAX_GAKUSEKI ; i++ ){ s−>gakuseki[i]=in[i]; if( in[i]==’\0’ ){ return( 1 ); } } s−>gakuseki[i−1] = ’\0’; return( 0 ); }
main() { Student *s1; s1 = (Student *)malloc(sizeof(Student)); set_gakuseki( s1, "982001" ); set_name( s1, "青野直樹" ); print_student( s1 ); }
main() { Student *s1; s1 = (Student *)malloc(sizeof(Student)); if( s1 == NULL ){ fprintf(stderr,"error in malloc\n"); exit(-1); } if( set_gakuseki( s1, "982001" ) == 0 ){ fprintf(stderr,"error in set_gakuseki\n"); exit(-1); } if( set_name( s1, "青野直樹" ) == 0 ){ fprintf(stderr,"error in set_name\n"); exit(-1); } print_student( s1 ); free(s1); }
# ./a.out < fileX
/* list0701.c */ #include <stdio.h> main() { char buff[BUFSIZ]; while( fgets(buff,BUFSIZ,stdin)!=NULL){ printf(”%s”,buff); } }
# ./a.out < list0701.c
022001 阿部泰洋 Yasuhiro Abe 鳥取県鳥取市湖山町南5丁目 19840302 abe@ike.tottori-u.ac.jp
実行の様子 # gcc prac0703.c # ./a.out < data.txt 学籍番号: 022001 名 前 : 阿部泰洋(Yasuhiro Abe) 生年月日: 1984年3月2日 住 所 : 鳥取県鳥取市湖山町南5丁目 E-Mail : abe@ike.tottori-u.ac.jp