C언어 텍스트파일 불러들이는 방법

C언어 텍스트파일 불러들이는 방법

작성일 2006.11.04댓글 1건
    게시물 수정 , 삭제는 로그인 필요

안녕하세요. 다름이아니오라 아래에있는 해시테이블 소스에 텍스트파일을읽어들이려고합니다. 문자열로 이루어진 test.txt 파일을 읽어들이려고하는데요 이책저책보고 네이버를 검색해서 저와비슷한 문제를 보고 시도를 해보았지만 제가 워낙 초보인관계로 어떻게 해야할지를 모르겠네요. ㅡ.ㅡ;;;

 

바로 아래보이는 소스에서 읽어들일려고하는데요 소스는 완벽한 소스일껍니다.

텍스트파일 읽어들이는 부분만 없어서 그렇지요.. ㅠㅠ 컴파일 하고 실행시키면

에러없이 실행됩니다. 하지만 입력값이 없으니 커서만 깜빡깜빡거리고 있네요..

 

해결책을 주시면 정말 감사하겠습니다. 소스코드가 70줄정도되네요.. ^^;;

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node *nodeptr;
typedef struct node {
 char *word;
 int count;
 nodeptr next;
} node;

#define NHASH 29989
#define MULT 31
nodeptr bin[NHASH];

unsigned int hash(char *p)
{ unsigned int h = 0;
 for ( ; *p; p++)
  h = MULT * h + *p;
 return h % NHASH;
}

#define NODEGROUP 1000
int nodesleft = 0;
nodeptr freenode;

nodeptr nmalloc()
{ if (nodesleft == 0) {
  freenode = malloc(NODEGROUP*sizeof(node));
  nodesleft = NODEGROUP;
 }
 nodesleft--;
 return freenode++;
}

#define CHARGROUP 10000
int charsleft = 0;
char *freechar;

char *smalloc(int n)
{ if (charsleft < n) {
  freechar = malloc(n+CHARGROUP);
  charsleft = n+CHARGROUP;
 }
 charsleft -= n;
 freechar += n;
 return freechar - n;
}

void incword(char *s)
{ nodeptr p;
 int h = hash(s);
 for (p = bin[h]; p != NULL; p = p->next)
  if (strcmp(s, p->word) == 0) {
   (p->count)++;
   return;
  }
 p = nmalloc();
 p->count = 1;
 p->word = smalloc(strlen(s)+1);
 strcpy(p->word, s);
 p->next = bin[h];
 bin[h] = p;
}

int main()
{ int i;
 nodeptr p;
 char buf[100];
 
 for (i = 0; i < NHASH; i++)
  bin[i] = NULL;
 while (scanf("%s", buf) != EOF)
  incword(buf);
 for (i = 0; i < NHASH; i++)
  for (p = bin[i]; p != NULL; p = p->next)
   printf("%s %d\n", p->word, p->count);
   
    return 0;
}


#c언어 텍스트파일 읽기 #c언어 텍스트파일 한줄씩 읽기 #c언어 텍스트파일 #c언어 텍스트파일 입출력 #c언어 구조체 텍스트파일

profile_image 익명 작성일 -

파일 오픈 하는 부분만 추가했습니다. __색

그리고 추가로 나타나는 에러(워닝) 수정했습니다. __색

 

#include
#include
#include

typedef struct node *nodeptr;
typedef struct node
{
    char *word;
    int count;
    nodeptr next;
} node;

#define NHASH 29989
#define MULT 31
nodeptr bin[NHASH];

unsigned int hash(char *p)
{
    unsigned int h = 0;
    for (; *p; p++)
        h = MULT * h + *p;
    return h % NHASH;
}

#define NODEGROUP 1000
int nodesleft = 0;
nodeptr freenode;

nodeptr nmalloc()
{
    if (nodesleft == 0)
    {
        freenode = (struct node *)malloc(NODEGROUP*sizeof(node));
        nodesleft = NODEGROUP;
    }
    nodesleft--;
    return freenode++;
}

#define CHARGROUP 10000
int charsleft = 0;
char *freechar;

char *smalloc(int n)
{
    if (charsleft     {
        freechar = (char *)malloc(n + CHARGROUP);
        charsleft = n + CHARGROUP;
    }
    charsleft -= n;
    freechar += n;
    return freechar - n;
}

void incword(char *s)
{
    nodeptr p;
    int h = hash(s);
    for (p = bin[h]; p != NULL; p = p->next)
        if (strcmp(s, p->word) == 0)
        {
            (p->count)++;
            return;
        }
        p = nmalloc();
        p->count = 1;
        p->word = smalloc(strlen(s) + 1);
        strcpy(p->word, s);
        p->next = bin[h];
        bin[h] = p;
}

int main()
{
    FILE *fp;
    int i;
    nodeptr p;
    char buf[100];
   
    for (i = 0; i         bin[i] = NULL;

    if((fp=fopen("test.txt","r"))==NULL)
    {
        printf("file open error\n");
        exit(-1);
    }
   
    while (fscanf(fp,"%s", buf) != EOF)
        incword(buf);
    fclose(fp);

    for (i = 0; i         for (p = bin[i]; p != NULL; p = p->next)
            printf("%s %d\n", p->word, p->count);
       
        return 0;
}

 

해당 글은 지식스폰서가 활동 기간 (04년~08년 6월 종료)중에 작성한 글 입니다.
  • 지식스폰서가 작성한 답변은 본문 내 자기소개 및 출처란의 실명, 상호명, URL표시를 허용합니다.
  • 출처란에 표시된 정보가 운영원칙에 위배되는 음란성, 불법성, 청소년 유해사이트 등으로 변질된 경우는 허용이 되지 않습니다.
지식스폰서란

C언어 텍스트 파일 안에 대문자만...

... 찾아내는 방법이 있습니다. 이 기능은 다음과 같이 단계를 나눌 수 있습니다. 1. 텍스트 파일을 열고 문자열을 읽어들인다. 2. 문자열을 한 글자씩...

c언어 텍스트파일 한줄씩

텍스트파일c언어로 불러오는데 1줄씩 불러와서 줄마다 들어온... 하는 방법 뿐인가요? ex) text 1 4 3 2 2 5 3 얻고싶은 형태 int arr[100][100]...

c언어에서 텍스트파일 입력하는 방법

텍스트 파일이 " 책이름, 책번호, 저자 " 이런... c언어를 배우긴 배웠는데, 파일쪽은 영... 모르는 파일을, 줄 단위로 불러들이는 것입니다. feof()가...

c언어 텍스트 파일 입출력 질문

파일 내용을 출력하려고 합니다. #include <stdio.h> #include <stdlib.h> int main() { char... 옳은 방법입니다. 조금 비유하자면 왼쪽에 있는 물건을 오른쪽으로 옮겨라...

C언어 텍스트파일 순차저장 방법

C언어에서 파일 순차저장하는 법좀 알려주세요 sprintf를 이용해서 할 수... 파일 입출력은 여러 가지 방법이 있습니다. - fopen() 함수를 이용하여...

c언어 텍스트파일 읽기

만약 택스트파일이 1.개 2.고양이 3 돼지 4.쥐 5.소 6.말 이렇개 돼있으면 짝수번째줄만 읽을 수 있는 방법이 있을까요 #define _CRT...