2011년7월14일_구조화된 파일에서 데이터를 획득하여 동적할당 구조체의 연결리스트를 만들어 보자.

 

 

● 파일에 내용을 기록하는 소스코드

   1:  #include <stdio.h>
   2:  #include <string.h>
   3:  //저수준 파일입출력 함수들을 사용하여 구조체를 기록하고 용량확인
   4:  #include <fcntl.h>
   5:  #include <sys/types.h>
   6:  #include <sys/stat.h>
   7:   
   8:  #include "node.h"
   9:   
  10:  int main()
  11:  {
  12:      int ifd;
  13:      int iCnt;
  14:      NODE sttemp;
  15:   
  16:      ifd = open("a.dat", O_TRUNC | O_CREAT | O_WRONLY, S_IWRITE);
  17:   
  18:      if(0 > ifd)
  19:      {
  20:          fprintf(stderr, "파일열기 에러!\n");
  21:          return -1;    //에러값
  22:      }
  23:   
  24:      for(iCnt = 0 ; 5 > iCnt ; ++iCnt)
  25:      {
  26:          printf("숫자를 입력하삼: ");
  27:          scanf("%d", &sttemp.iNum);
  28:          write(ifd, &sttemp, sizeof(sttemp));
  29:      }
  30:   
  31:      close(ifd);
  32:   
  33:      return 0;
  34:  }


<실행결과>

image 



● a.dat를 읽어와 동적할당 구조체들을 생성하고 그 구조체를 연결하여 단방향 연결리스트를 만드는 소스코드

   1:  #include <stdio.h>
   2:  #include <stdlib.h>
   3:  #include <string.h>
   4:  #include <fcntl.h>
   5:  #include <sys/types.h>
   6:  #include <sys/stat.h>
   7:  #include "node.h"
   8:   
   9:  int main()
  10:  {
  11:      int ifd;
  12:      int iCnt;
  13:      int iRet;
  14:      NODE *stpNode;            //st는 struct, p는 포인터
  15:      NODE *stpHead;
  16:          
  17:      ifd = open("a.dat", O_RDONLY);
  18:   
  19:      if(0 > ifd)
  20:      {
  21:          write(2, "파일열기에러!\n", 14);
  22:          return -1;
  23:      }
  24:      
  25:      //첫 할당 구조체
  26:      stpNode = malloc(sizeof(NODE));
  27:   
  28:      if(0 == stpNode)
  29:      {
  30:          write(2, "동적할당에러!\n", 14);
  31:          return -1;
  32:      }
  33:      
  34:      iRet = read(ifd, stpNode, sizeof(NODE));
  35:      stpNode->next = 0;
  36:      
  37:      if(0 == iRet)
  38:      {
  39:          free(stpNode);
  40:          write(2, "파일읽기에러!\n", 14);
  41:      }
  42:      else
  43:      {
  44:          stpHead = stpNode;
  45:      }
  46:      
  47:      // 2 ~ 5번째 동적할당 구조체
  48:      for(iCnt = 0 ; 4 > iCnt ; ++iCnt)
  49:      {
  50:          stpNode = malloc(sizeof(NODE));
  51:   
  52:          if(0 == stpNode)
  53:          {
  54:              write(2, "동적할당에러!\n", 14);
  55:              return -1;
  56:          }
  57:      
  58:          iRet = read(ifd, stpNode, sizeof(NODE));
  59:          stpNode->next = 0;
  60:      
  61:          if(0 == iRet)
  62:          {
  63:              free(stpNode);
  64:              write(2, "파일읽기에러!\n", 14);
  65:          }
  66:          else
  67:          {
  68:              stpNode->next = stpHead;        //1번 이전 구조체 가리킴.
  69:              stpHead = stpNode;                //2번 새로운 구조체 가리킴.
  70:          }
  71:      }
  72:   
  73:      //출력
  74:      while(0 != stpHead)
  75:      {
  76:          printf("%2d -> ", stpHead->iNum);
  77:          stpHead = stpHead->next;
  78:      }
  79:      
  80:      printf("NULL\n");
  81:      
  82:      close(ifd);
  83:      
  84:      //메모리 해제를 반복문으로 바꿀 것.
  85:      free(stpNode->next->next->next->next);
  86:      free(stpNode->next->next->next);
  87:      free(stpNode->next->next);
  88:      free(stpNode->next);
  89:      free(stpNode);
  90:      
  91:      return 0;
  92:  }


<실행결과>

image



● 연결리스트의 모양

image


● 연결

image