1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LEN 20
 
 
typedef struct _Student Student;
struct _Student{
    char name[MAX_NAME_LEN+1];
    int age;
    int num;
};
 
void store_students();
void search_student();
 
int main(){
    store_students();
    search_student();
    return 0;
}
 
void store_students(){
    Student base[4]={
        {"홍길동",20,3},{"감감찬",30,4},{"김유신",70,1},{"이순신",35,2}};
    FILE *fp=fopen("students.dat","wb");
    fwrite(base,sizeof(Student),4,fp);
    fclose(fp);
}
 
void search_student()
{
    FILE *fp=fopen("students.dat","rb");
    fseek(fp,0,SEEK_END);
    int cnt=ftell(fp)/sizeof(Student);
    rewind(fp);
 
    printf("cnt=%d\n",cnt);
    int nth=0;
    printf("원하는 순번(1~%d):",cnt);
    scanf("%d",&nth);
 
    if((nth<=0)||(nth>cnt))
    {
        printf("cnt=%d\n",cnt);
        printf("???:error");
        return;
    }
 
    fseek(fp,(nth-1)*sizeof(Student),SEEK_SET);
    Student student;
    fread(&student,sizeof(Student),1,fp);
 
    fclose(fp);
    printf("이름:%s 번호:%d 나이:%d\n",student.name,student.num,student.age);
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

+ Recent posts