반응형

소규모의 데이터베이스 프로그램 작성, 자기가 소유하고 있는 도서를 관리하는 프로그램을 작성하여 보자. 다음과 같은 메뉴 화면을 가진다.


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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
typedef struct BOOK {
    char name[SIZE];
    char author[SIZE];
    char company[SIZE];
} book;
int menu(void);
void add(FILE *fp);
void find(FILE *fp);
int main(void)
{
    int num = 0;
    FILE *fp = NULL;
    if ((fp=fopen("booklist.bin","a+"))==NULL)
    {
        printf("파일오픈실패\n");
        exit(1);
    }
    while (1)
    {
        num = menu();
        if (num == 1)
            add(fp);
        else if (num == 2)
            find(fp);
        else
            break;
    }
    fclose(fp);
    return 0;
}
int menu(void)
{
    int i = 0;
    printf("메뉴\n");
    printf("1. 추가\n");
    printf("2. 탐색\n");
    printf("3. 종료\n");
    printf("번호를 입력하세요: ");
    scanf("%d"&i);
    getchar();
    return i;
}
void add(FILE *fp)
    book a;
    printf("도서의 이름: ");
    gets(a.name);
    printf("저자: ");
    gets(a.author);
    printf("출판사명: ");
    gets(a.company);
    fseek(fp, 0, SEEK_END);
    fwrite(&a, sizeof(book), 1, fp);
}
void find(FILE *fp)
{
    char arr[SIZE];
    book find;
    printf("도서의 이름을 입력하세요: ");
    gets(arr);
    fseek(fp, 0, SEEK_SET);
    while (!feof(fp))
    {
        fread(&find, sizeof(book), 1, fp);
        if (strcmp(find.name,arr)==0)
        {
            printf("저자: %s\n",find.author);
            printf("출판사명: %s\n",find.company);
            break;
        }
    }
}
cs




반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C언어' 카테고리의 다른 글

특정 단어 찾기  (0) 2017.02.18
텍스트 파일과 이진 파일  (0) 2017.02.17
줄 번호 붙이기  (0) 2017.02.17
사용자 입력 텍스트 파일 저장  (0) 2017.02.13
인쇄 가능 문자 수 세기  (0) 2017.02.13

+ Recent posts