게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
도서관리 프로그램을 짜봤습니다 조언 부탁 드립니다.
게시물ID : programmer_17687짧은주소 복사하기
작성자 : 자르반고등어
추천 : 0
조회수 : 574회
댓글수 : 8개
등록시간 : 2016/06/20 22:56:10
옵션
  • 본인삭제금지
#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996) //scanf 함수 및 gets 함수를 쓰기위해서 선언함 2013버전은 이 함수가 없으면 못쓰게 하더라구요

struct information
{
char name[100];
char writer[50];
char no[6];
}info[100]; //등록할때 저장하기 위해서 사용하는 임시 버퍼

struct buffer
{
char name[100];
char wrtiter[50];
char no[6];
}buf[100]; //삭제할때 데이터를 임시로 담아두는 버퍼 

void information();
void enrollment();
void delete();
void search();

int info_index; //자료를 불러 올때 자료의 수를 세기 위한 변수

int main()
{
int b; //번호를 선택하기 위해 선언된 변수

printf("무슨 작업을 하시겠습니까?\n 1.자료현황\n 2.자료등록\n 3.자료삭제\n 4.자료검색\n 5.종료\n ->");
scanf("%d", &b);
fflush(stdin); //숫자를 대입하고 엔터를 치면 엔터 까지 스트림에 포함되므로 버퍼를 비워줘야 한다. 공통 스트림은 다음 입력에 영향을 미치기 때문
switch (b)
{
case 1:
information();
break;
case 2:
enrollment();
break;
case 3:
delete();
break;
case 4:
search();
break;
case 5:
break;
}

return 0;

}

void information() //저장된 책들의 정보를 불러 옵니다. 
{
//int result= 0;
int count = 0; 
info_index = 0; //0부터 채우기 위해 0으로 초기화 이 이후 
FILE* stream1;
    stream1 = fopen("information.txt", "r"); //stream1을 읽기 형식으로 선언함
if (stream1 == NULL)
{
printf("파일열기 에러\n");
main(); //stream1 이 빈 스트림이라면 파일열기 에러를 띄운후 main으로 복귀
}
while (1)
{
printf("%d \n", count + 1);
fgets(info[info_index].name, sizeof(info[info_index].name),stream1);
printf("책 제목: %s", info[info_index].name);
fgets(info[info_index].writer, sizeof(info[info_index].writer), stream1);
printf("저자: %s", info[info_index].writer);
fgets(info[info_index].no, sizeof(info[info_index].no), stream1);
printf("일련번호: %s\n", info[info_index].no);
info_index++;
count++;
if (feof(stream1))
{
break; //스트림이 끝에 다다르면 while문을 빠져 나감 
}}
fclose(stream1); // 스트림을 닫는다.
main(); //main 함수로 복귀 
}

void enrollment() //책을 등록합니다. 
{
FILE* stream;
stream = fopen("information.txt", "a"); //파일 스트림을 add 형식으로 불러옵니다.
char name_buffer[100];
char number_buffer[100];
char writer_buffer[100]; //3개 각각 이름, 번호, 저자의 정보를 임시로 담아두는 버퍼입니다. 
char i; //등록을 계속할지 묻는 선택지에서 문자를 받기위해 선언한 변수입니다.

printf("책 제목:");
fgets(name_buffer, sizeof(name_buffer), stdin);
fflush(stdin);
fputs(name_buffer, stream); 

printf("책 저자:");
fgets(writer_buffer, sizeof(writer_buffer), stdin);
fflush(stdin);
fputs(writer_buffer, stream);

printf("일련번호");
fgets(number_buffer, sizeof(number_buffer), stdin);
fflush(stdin);
fputs(number_buffer, stream);
fclose(stream); //버퍼를 통해 받은 정보를 스트림을 통해 메모장에 저장합니다.
printf("계속합니까? Y/N");
scanf("%s", &i);
fflush(stdin);
switch (i)
{
case 'Y':
case 'y':
enrollment();
case'N':
case'n':
break;
}   //i를 통해 문자를 받고 받은 문자의 형태에 따라 등록을 계속할지 반복문을 빠져나갈지 정합니다. 
main(); //반복문을 나가서 main 함수를 불러 옵니다.
}

void delete() //삭제는 기존의 자료를 모두 삭제하고 선택한 하나의 항목을 제외한 나머지 애들을 새롭게 써주는 형식으로 했습니다. 
{
FILE* stream1;
FILE* stream2; //스트림2 는 w 형태로 뒤에서 선언합니다. 
FILE* stream3;
stream1 = fopen("information.txt", "r"); //읽기 형식으로 자료를 불러옵니다.
stream3 = fopen("information.txt", "a"); //새롭게 자료를 적어야 하므로 add 형식으로 선언합니다.
char name_buffer[100]; //비교를 하기 위해 선언한 변수입니다.
int result = 0;
int count = 0;
int count1 = 0;
int i = 0;
info_index = 0;
char j;
while (1) //기존의 정보 불러오기를 통해 내용을 불러 옵니다. 
{
fgets(info[info_index].name, sizeof(info[info_index].name), stream1);
fgets(info[info_index].writer, sizeof(info[info_index].writer), stream1);
fgets(info[info_index].no, sizeof(info[info_index].no), stream1);
if (feof(stream1))
{
break;
}
info_index++;
}
printf("책 제목의 일부 혹은 전체를 적어주세요:");
gets(name_buffer); //버퍼를 통해 찾고자 하는 책의 정보를 받습니다.
fflush(stdin);
while (i<info_index)
{
result = strncmp(name_buffer, info[count].name, !NULL); //!NULL 을 하니까 두개의 비교 배열의 NULL문자 까지만 계산 한다
if (result == 0)
{
printf("제목:%s\n저자:%s\n일련번호:%s\n", info[count].name, info[count].writer, info[count].no);
break;

count++;
i++;
}// 버퍼에 받은 정보와 불러올때 구조체에 저장된 각 변수를 비교합니다. result가 0이 되었을때 스탑하고 그 숫자의 정보들을 불러옵니다.
if (i == info_index)
{
printf("검색한 책이 없습니다.\n\n");
main();
}//i가 info_index와 수가 같으면 루프가 모두 돌았다는 뜻으로 자료가 없음을 의미합니다. 
printf("삭제 하시겠습니까? Y/N");
scanf("%c", &j);
fflush(stdin);
switch (j)
{
case 'Y':
case 'y':
while (1)
{
strcpy(buf[count1].name, info[count1].name); //이렇게 바꿔 줘야 I-Value 오류가 안뜸
//buf[count].name[sizeof(buf[count1].name) - 1] = 0;
strcpy(buf[count1].wrtiter, info[count1].writer);
//buf[count].wrtiter[sizeof(buf[count1].wrtiter) - 1] = 0;
strcpy(buf[count1].no, info[count1].no);
//buf[count].no[sizeof(buf[count1].no) - 1] = 0;
count1++;
if (count1==info_index)
{
break;
}
} //기존에 저장되어 있는 정보들을 임시버퍼로 옮깁니다. 
count1 = 0;//처음 세었던 count1을 다시 처음부터 세기 위해 0으로 초기화 합니다.
i = 0;//i도 마찬가지로 세는 개념이므로 0으로 초기화 합니다. 
stream2 = fopen("information.txt", "w"); //write 를 선언함으로써 메모장에 저장되어 있던 자료가 모두 사라집니다.
while (i<info_index)
if (count1 == count)
{    
count1++;
                    i++;
continue;
}
fputs(buf[count1].name, stream3);
fflush(stream3);
fputs(buf[count1].wrtiter, stream3);
fflush(stream3);
fputs(buf[count1].no, stream3);
fflush(stream3);
count1++;
} //stream3의 add 기능을 사용해서 버퍼에 저장된 자료들을 다시 처음부터 메모장에 적되 처음에 지정한 수와 같으면 그 수는 제외하고 정보를 저장합니다.
fclose(stream3);
break;
case 'N':
case 'n':
break;
}
main();//삭제를 끝마치고 main 함수로 돌아갑니다. 
}

void search()
{
char name_buffer[100];
char number_buffer[100];
char writer_buffer[100];//각 정보를 받기 위한 임시변수입니다.

int select; //검색할때 검색방식 숫자를 정하기위해 선언한 변수
int result=0;//검색할때 버퍼에 저장된 정보와 각 구조체에 저장된 변수를 비교하고 값을 받기 위해 선언한 변수
int count=0; // 검색할때 기존에 저장된 정보를 넘기기 위해 선언된 변수
int i=0;
info_index = 0; //전역변수로 설정해 놓음, 자료의 총 수를 세기 위한 변수
FILE* stream;
stream = fopen("information.txt", "r"); //스트림을 읽기로 선언
while (1)
{
fgets(info[info_index].name, sizeof(info[info_index].name), stream);
fgets(info[info_index].writer, sizeof(info[info_index].writer), stream);
fgets(info[info_index].no, sizeof(info[info_index].no), stream);
if (feof(stream))
{
break;
}
info_index++;
} //자료를 불러오고 info_index 변수에 각 숫자를 저장합니다.
printf("검색 방법을 설정해 주세요\n1.제목\n2.작가\n3.일련번호\n->");
scanf("%d", &select);
fflush(stdin);
switch (select)
{   
case 1:
printf("책 제목의 일부 혹은 전체를 적어주세요:");
gets(name_buffer);
fflush(stdin);
while (i<info_index)
{
result = strncmp(name_buffer, info[count].name, !NULL); //!NULL 을 하니까 두개의 비교 배열의 NULL문자 까지만 계산 한다
if (result == 0)
{
printf("제목:%s\n저자:%s\n일련번호:%s\n", info[count].name, info[count].writer, info[count].no);
break;
}

count++;
i++;
}// 버퍼에 받은 정보와 불러올때 구조체에 저장된 각 변수를 비교합니다. result가 0이 되었을때 스탑하고 그 숫자의 정보들을 불러옵니다.
if (i == info_index)
{
printf("검색한 책이 없습니다.\n\n");
break;
}//숫자가 다 돌아서 i 와 info_index가 같으면 자료가 없는것이나 마찬가지 이므로 다음 문구를 띄웁니다.
break;
case 2:
printf("저자의 이름 혹은 일부를 적어주세요 :");
gets(writer_buffer);
fflush(stdin);
while (i<info_index)
{
result = strncmp(writer_buffer, info[count].writer, !NULL); //!NULL 을 하니까 두개의 비교 배열의 NULL문자 까지만 계산 한다
if (result == 0)
{
printf("제목:%s\n저자:%s\n일련번호:%s\n", info[count].name, info[count].writer, info[count].no);
break;
}

count++;
i++;
}
if (i == info_index)
{
printf("검색한 작가가 없습니다.\n\n");
            break;
}
break;
case 3:
printf("일련번호를 적어주세요");
gets(number_buffer);
fflush(stdin);
while (i<info_index+1)
result = strncmp(number_buffer,info[count].no,4);
            if (result == 0)
{
printf("%s %s %s\n", info[count].name, info[count].writer, info[count].no);
break;
}
count++;
i++;
}
} //이후의 방식도 제목 검색과 같습니다. 
fclose(stream);
main();// 검색을 끝내고 main 함수로 돌아갑니다.
}


주석을 단다고 달았는데 아직 모자란 게 많이 있을 것 같습니다. 

단지 문제가 있다면 삭제를 할때 디버깅 오류가뜨고 저장되어 있는 메모장에 들어가면 지정한 자료는 사라지면서 밑에 깨진 글씨들이 주루룩 뜨네요
이걸 이유를 잘 모르겠습니다.

전체적으로 문제점 지적 해주시면 감사하겠습니다. 
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호