링크드리스트 프로그래밍~...ReadFile() 오류부분 왜 그럴까요? 고수님들 해결부탁드려요~

wunseo의 이미지

음... 다른 건 잘 되는데 ReadFile()함수 파일에서 받아오기 하면 왜 안될까요?

ㅡㅡ;

#include
#include
#include
#include
#include

typedef struct _Node {
int KEY;
char NAME[10];
struct _Node *next;
} NODE;

NODE *head, *end;
void PrintTitle(void);
void PrintMenu(void);
void InputMenu(void);
void InitializeNode(void);
void InsertNode(NODE *);
void InsertNode2(int, char**);
void DeleteNode(NODE *);
void PrintNode(void);
void SearchNode(NODE *);
void ReadFile(char*);
void WriteFile(char*);
void Cleanup(void);

int main(void)
{

int i;
PrintTitle();
InitializeNode();

while(1)
{
PrintMenu();
InputMenu();
}

}

void PrintTitle(void)
{
printf("\n\n****************************************\n");
printf("** **\n");
printf("** Linked List test **\n");
printf("** **\n");
printf("** **\n");
printf("****************************************\n\n\n");
}

void PrintMenu(void)
{

printf("\n\n\n0. Initialize Node\n");
printf("1. Insert Node\n");
printf("2. Delete Node\n");
printf("3. Print Node\n");
printf("4. Search Node\n");
printf("5. Read File\n");
printf("6. Write File\n");
printf("Q. Quit Program\n");
printf("Input the number please : ");
}

void InputMenu(void)
{
char i;
char fname[50];
NODE *ptr;
ptr=(NODE*)malloc(sizeof(NODE));

i=getchar();

switch (i)
{
case '0' :
Cleanup();
InitializeNode();
printf("Node is cleaned\n");
PrintNode();
break;

case '1' :
PrintNode();
printf("Input the KEY and Name : ");
if(scanf("%d%s", &ptr->KEY, ptr->NAME)==2)
{
InsertNode(ptr);
PrintNode();
break;
}
else
{
printf("Usage : (int)KEY (string)NAME\n\n");
break;
}

case '2' :
PrintNode();
printf("Input the KEY :");
scanf("%d", &ptr->KEY);
DeleteNode(ptr);
PrintNode();
break;

case '3' :
PrintNode();
break;

case '4' :
printf("Input the NAME : ");
scanf("%s", ptr->NAME);
SearchNode(ptr);
break;

case '5' :
Cleanup();
InitializeNode();
printf("Input the FILENAME :");
scanf("%s",fname);
ReadFile(fname);
break;

case '6' :
printf("Input the FILENAME :");
scanf("%s",fname);
WriteFile(fname);
break;

case 'Q' :
printf("\nThanks! Good bye!\n\n");
exit(0);
break;

default :
break;
}

}

void InitializeNode(void)
{
head=(NODE*)malloc(sizeof(NODE));
end=(NODE*)malloc(sizeof(NODE));
head->next=end;
end->next=end;
}

void InsertNode(NODE *ptr)
{
NODE *indexptr;

for(indexptr=head; indexptr!=(end);indexptr=indexptr->next)
{
if(indexptr->next==end)
{
ptr->next=indexptr->next;
indexptr->next=ptr;
printf("\n\tKEY %d\tNode %s is inserted!!\n", ptr->KEY,ptr->NAME);
break;
}

else if(indexptr->next->KEY > ptr->KEY)
{
ptr->next=indexptr->next;
indexptr->next=ptr;
printf("\n\tKEY%d\tNode %s is inserted!!\n", ptr->KEY,ptr->NAME);
break;
}
}
}

void DeleteNode(NODE *ptr)
{
NODE *indexptr;
NODE *deleteptr;

for (indexptr=head; indexptr!=end;indexptr=indexptr->next)
if(indexptr->next->KEY == ptr->KEY) {
deleteptr=indexptr->next;
break;
}

printf("\n%d Node : %s is deleted!!", deleteptr->KEY,deleteptr->NAME);
indexptr->next=indexptr->next->next;
free(deleteptr);
}

void PrintNode(void)
{
NODE *indexptr;
printf("\n\tKEY \tNAME\n");
printf("\t--------------------\n");

for(indexptr=head->next; indexptr!=end;indexptr=indexptr->next)
{
printf("\t%-2d \t%-2s\n",indexptr->KEY, indexptr->NAME);
}
printf("\n");

}

void SearchNode(NODE *ptr)
{
NODE *indexptr;
for(indexptr=head; indexptr!=end; indexptr=indexptr->next)
{
if (strcmp(indexptr->next->NAME, ptr->NAME)==0)
{
printf("%s's is located in this Linked List.\nKEY number is %d.\n\n",indexptr->next->NAME, indexptr->next->KEY);
break;
}
else
{
printf("%s's is NOT located in this Linked List.\n\n",ptr->NAME);
break;
}

}
}

void ReadFile(char fname[])
{
NODE *ptr;
ptr=(NODE*)malloc(sizeof(NODE));
FILE *fp;
int i=1;

fp=fopen(fname,"r");
if(!fp)
{
fprintf(stderr,"Error : couldn't read %s file\n",fname);
exit(1);
}
while(fscanf(fp, "%d %sn", &ptr->KEY, ptr->NAME)!=EOF){
InsertNode(ptr);
printf("\t%d %s\n", ptr->KEY, ptr->NAME);
}
fclose(fp);
printf("\n%s File is read!!", fname);
}

void WriteFile(char fname[])
{
NODE *indexptr;
FILE *fp;

fp=fopen(fname,"w");
if (!fp)
{
fprintf (stderr, "Error: couldn't write %s file\n", fname);
exit(1);
}

for (indexptr=head->next;indexptr!=end;indexptr=indexptr->next)
fprintf(fp,"%d %s\n",indexptr->KEY,indexptr->NAME);

fclose(fp);
printf("\n%s File is wrote!!", fname);
}

void Cleanup(void)
{
NODE *indexptr;
NODE *deleteptr;

for(indexptr=head->next;indexptr!=end;indexptr=indexptr->next)
{
deleteptr=indexptr;
head->next=indexptr->next;
printf("KEY : %d\tNAME : %s CLEANED\n",deleteptr->KEY, deleteptr->NAME);
free(deleteptr);

}
free(head);
free(end);

}

gurumong의 이미지

아래 코드 처럼 code /code로 둘러쌓이게 해서 들여쓰기가 무시되지 않게 해주세요
보기가 무척이나 힘들어요;

아마 이 부분이 문제인거 같은데요
포인터를 선언해놓구선 초기화 없이 임의메모리에 접근을 하네요

void InsertNode(NODE *ptr)
{
    NODE *indexptr;
 
    for(indexptr=head; indexptr!=(end);indexptr=indexptr->next)
bushi의 이미지

ReadFile() 안의 malloc() 위치가 틀렸습니다.
한번 malloc() 해서 그걸로 계속 주구장창 InsertNode() 하고 있습니다.
link 가 대박 꼬이게 됩니다.

OTL

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.