[C언어]도서관프로그램

fullplatex의 이미지

// C언어 초보입니다. 도서 하나를 이름과 작가 메모를 입력해서
출력하게 하는 연습인데 제목이랑 작가, 메모를 입력하면 죽어버리네요!! ㅜㅜ
무엇이 문제일까요?!! 도와주세요 //

#include
#include
#include
#pragma warning(disable:4996)
#define TMAX 30
#define WMAX 20
#define MMAX 50

struct BOOK
{
char name_of_book[TMAX];
char name_of_writer[WMAX];
char memo[MMAX];
};

int main(void)
{
int ing = 0;
int cmd;
struct BOOK *book = malloc(sizeof(struct BOOK));

while (1) {

printf("0.종료 1.도서목록 2.도서추가 3.도서삭제\n\n");

printf("명령:");
scanf("%d", &cmd);

switch (cmd)
{

case 0:
exit(1);

case 1:
bview(book, &ing);
break;

case 2:
badd(book, &ing);
break;


}

return 0;
}
}

int bview(struct BOOK *b1, int *ing)
{

printf("%s\n", b1->name_of_book);
printf("%s\n", b1->name_of_writer);
printf("%s\n", b1->memo);

}

int badd(struct BOOK *b1, int *ing)
{
printf("도서제목:");
scanf_s("%s", b1->name_of_book, sizeof(struct BOOK));

printf("저자이름:");
scanf_s("%s", b1->name_of_writer, sizeof(struct BOOK));

printf("메모:");
scanf_s("%s", b1->memo, sizeof(struct BOOK));

ing += 1;

}

raymundo의 이미지

입력받다 죽는 건 많은 경우 메모리에서 할당받은 유효한 공간을 벗어나서 값을 쓰거나 읽으려고 해서일 때가 많으니 그것 위주로 살펴보시고요.

scanf_s("%s", b1->name_of_book, sizeof(struct BOOK));
 
// b1->name_of_book 은 크기가 TMAX 바이트밖에 안 되는데
// 그 다음 인자는그보다 훨씬 큰 struct BOOK의 크기로 하셨네요.
// sizeof(b1->name_of_book) 으로 고쳐보시죠. 다른 scanf_s도 같은 방식으로.
// 그리고 입력할 때 한글은 한 글자에 2바이트를 차지할 테니(UTF-8을 쓴다면 3바이트)
// 조금만 길게 입력해도 죽게 될 것 같습니다.

좋은 하루 되세요!

세벌의 이미지

https://kldp.org/node/158191 참고해서 소스코드 보여주셔요.
컴파일은 되는데 실행하면 죽는다면, 컴파일할 때 나오는 Error 뿐 아니라 Warning 도 잘 보셔요.
메모리 관련 경고가 있을 겁니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.