파일 <dbcreate.c>

# 이게 입력받는 코딩


1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include "student.h"
6
7 int main(int argc,char *argv[])
8 {
9      int fd;
10      struct student rec;
11 if (argc < 2) {
12      fprintf(stderr, "instruction: %s file ",argv[0]);
13      exit(1);
14 }
15      if ((fd = open(argv[1], O_WRONLY | O_CREAT | O_EXCL, 0640)) == 3) {
16           lseek(fd, (rec.id - START_ID) * sizeof(rec), SEEK_SET);
17           write(fd, (char *) &rec, sizeof(rec) );
18 }
19      close(fd);
20      exit(0);
21 }
~
~


#이게 찾는 코딩인데

파일 <dbserach.c>


1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5
6 #include "student.h"
7
8 int main(int argc, char *argv[])
9 {
10      int fd, id;
11      char c;
12      struct student rec;
13 if (argc < 2) {
14      fprintf(stderr,"instruction : %s file ", argv[0]);
15      exit(1);
16 }
17      if ((fd = open(argv[1],O_RDONLY)) == -1) {
18           perror(argv[1]);
19           exit(2);
20 }
21
22 do {
23      printf(" do you want search a studebt id: ");
24      i f (scanf("%d", &id) == 1) {
25           lseek(fd,(id-START_ID)*sizeof(rec), SEEK_SET);
26      if ((read(fd, (char *) &rec, sizeof(rec)) > 0 && (rec.id != 0)))
27           printf("name:%s id:%d score:%d ",rec.name, rec.id, rec.score);
28
29 else
30      printf("record %d not here ",id);
31 } else printf("input error ");
32          printf("cotinue? (y/n) ");
33           scanf(" %c", &c);
34 } while (c=='y');
35      close(fd);
36      exit(0);
37 }
~
~
~

#이건 student.h


1 #define MAX 24
2 #define START_ID 1401001
3
4 struct student {
5      char name[MAX];
6      int id;
7      int score;
8 };
~
~


이렇게 있다고 햇을때 학번 이름을 입력하려고 하면 어떻게 해되죠???

저는 파일안에 파일이름 studentlist

이런식으로 만들고 안에

201904007 김개붕 64 만들고

gcc -o dbcre dbcreate.c

gcc -o dbsre dbserach.c 하고나서


./dbcre studentlist 하고

./dbsre studentlist 한다음

id: ? 201904007 치면

찾을줄 알앗는데 못찾네요 왜이러죠