게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[질문/C] 저수준 파일 입출력
게시물ID : programmer_13924짧은주소 복사하기
작성자 : ozum2
추천 : 0
조회수 : 499회
댓글수 : 2개
등록시간 : 2015/10/16 19:51:59
옵션
  • 베스트금지
  • 본인삭제금지
저수준 파일입출력을 사용해서 파일 속 단어를 정렬하는 프로그램입니다.
고수준 파일입출력을 사용할 때와 같은 연결리스트 코드를 넣어 파일끝까지 글자를 확인해 단어로 만들어 연결리스트에 넣는 방법입니다.
문제는 고수준 파일입출력에서 EOF까지 읽어서 insert_word를 해주면 이상없이 잘 작동하지만
저수준 파일입출력에서 read를 사용해 0까지 가게되면 insert_word가 다 작동되다가 마지막에 Segmentation fault 가 납니다.
Segmentation fault 가 포인터를 잘못 사용하게 되면 뜬다고 하던데 고수준에서는 되던게 저수준에서는 잘되다 마지막에만 안되
어떻게 처리해야할지 감이 잡히지 않습니다.
코드가 너저분하고 쓸데없이 길어도 한번 봐주시면 감사하겠습니다ㅜㅜ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
 
typedef struct Word {
    char word[BUFSIZ];
    int count;
    struct Word *pre;
    struct Word *next;
} Word;
 
Word *create_word(char *s)
{
    Word *new_word;
    new_word = (Word *)malloc(sizeof(Word));
    if(new_word==NULL) printf("error malloc\n");
    strcpy(new_word->word, s);
    new_word->count = 0;
    new_word->pre = NULL;
    new_word->next = NULL;
 
    return new_word;
}
 
void insert_word(Word **head, char *w)
{
    Word *new_word = create_word(w);
    Word *= *head;
 
    if(*head == NULL)
    {
        new_word->count++;
        new_word->pre = *head;
        *head = new_word;
    }
    else if(strcmp(s->word, new_word->word)==1)
    {
        new_word->count++;
        new_word->pre = *head;
        new_word->next = s;
        s->pre = new_word;
        *head = new_word;
    }
    else
    {
        while(s->next != NULL)
        {
            if(strcmp(s->word, new_word->word)==-1)
            {
                s = s->next;
            }
            else
                break;
        }
        if(strcmp(s->word, new_word->word)==-1)
        {
            new_word->count++;
            new_word->pre = s;
            s->next = new_word;
        }
        else if(strcmp(s->word, new_word->word)==1)
        {
            s->pre->next = new_word;
            new_word->pre = s->pre;
            new_word->count++;
            new_word->next = s;
            s->pre = new_word;
        }
        else if(strcmp(s->word, new_word->word)==0)
        {
            s->count++;
            free(new_word);
        }
    }
}
 
main()
{
    int fdr, fdw;
    Word *word;
    int c;
    char ch, buf, *buf_out, buffer[BUFSIZ];
    off_t cur, end;
 
    if((fdr=open("the_gold_bug.txt", O_RDONLY))==-1)
    {
        perror("open : the_gold_bug.txt");
        exit(1);
    }
 
    if((fdw=open("text.res2", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))==-1)
    {
        perror("open : text.res2");
        exit(1);
    }
 
    while(read(fdr, &buf, 1)>0)
    {
        c = toascii(buf);
 
        if((c>='a' && c<='z'|| (c>='A' && c<='Z'))
        {
            if(c>='A' && c<='Z')
                c+=32;
            sprintf(&ch, "%c", c);
            if(*buffer==0)
                strcpy(buffer, &ch);
            else
                strcat(buffer, &ch);
        }
        else
        {
            if(*buffer != 0)
            {
                insert_word(&word, buffer);
            }
            memset(buffer, '\0'sizeof(buffer));
        }
    }
 
    while(word)
    {
        sprintf(buf_out, "%s\t%d\n", word->word, word->count);
        write(fdw, buf_out, strlen(buf_out)*sizeof(char));
        word = word->next;
    }
 
    close(fdr);
    close(fdw);
}
 
 
 
 

cs
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호