파일을 한줄단위로 읽어주는 함수에서 Segmentation fault가 납
글쓴이: zkjinz / 작성시간: 일, 2004/12/12 - 4:49오후
안녕하세요.. 가입하고 처음 올리는글이 질문이라니.. 조금 염치없나요..? 죄송합니다.. ^^;;
다름이 아니라.. 텍스트로 만들어진 애니메이션을 출력해주는 아주 간단한 프로그램을 만들고있는데요..
예전에 만들어 놓은 파일을 한줄단위로 읽는 함수를 그대로 쓰는데.. 어쩐일인지 여기서 segmentation fault가 나네요.. 컴파일은 잘되구요..
고수님들 도와주세요~
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int readline(int fd, char *line, int length);
main(int argc, char *argv[])
{
int fd; //file descriptor
int read_byte;
char *frame, *title;
char buf[256];
if(argc != 1)
{
printf("usage : playmovie\n");
exit(1);
}
printf("Please enter the title : ");
fgets(buf, 256, stdin); // gets input(movie title) from the user
title = strtok(buf, "\n");
printf("file name : %s\n", title);
if( (fd = open(title, O_RDONLY)) != -1)
{
while(1)
{
//printf(" 1 ");
if( (read_byte = readline(fd, frame, 1024)) > 0 )
//if( (read_byte = read(fd, frame, 128)) > 0 )
{
//printf(" 6 ");
if( strcasecmp(frame, "end") == 0 )
{
printf("\n\n\n");
sleep(1);
}
else if( strcasecmp(frame, "stop") == 0 )
{
break;
}
else
{
//printf(" 7 ");
printf("%s\n", frame);
}
}
else if(read_byte == 0)
{
printf("\nEND OF THE FILE\n");
break;
}
else if(read_byte == -9999)
{
printf("%d\n", read_byte);
}
else
{
printf("Error occured : Content of file cannot be read.\n");
printf("error : %s\n", strerror(errno));
break;
}
}// end of while
}
else
{
printf("Error occured : File cannot be opened.\n");
exit(1);
}
close(fd);
}
int readline(int fd, char *line, int length)
{
int i, rb;
char letter;
printf(" 2 ");
//return -9999;
for(i = 1 ; i < length ; i++)
{
printf(" 3 ");
if( (rb = read(fd, &letter, 1)) == 1 )
{
printf(" 4 ");
*line++ = letter;
if(letter == '\n')
{
break;
}
}
else if(rb == 0)
{
if(i == 1)
{
return(0);
}
else
{
break;
}
}
else
{
return(-1);
}
}
printf(" 5 ");
*line = '\0';
return(i);
}
그리고 gdb로 보면 아래처럼 나오는데.. *line = letter; 가 틀린건가요?
Quote:
#0 0x080487a1 in readline (fd=3, line=0x4000cc60 "U\211?VS?U", length=1024)
at pmt.c:103
103 *line++ = letter;
(gdb) bt
#0 0x080487a1 in readline (fd=3, line=0x4000cc60 "U\211?VS?U", length=1024)
at pmt.c:103
#1 0x08048621 in main (argc=1, argv=0xbfffe404) at pmt.c:40
#2 0x42015704 in __libc_start_main () from /lib/tls/libc.so.6
긴글 읽어주셔서 감사합니다.. 좋은하루 되세요.. 8)
Forums:


변수 frame에 메모리는 어디에서 할당하나요?아무리 봐도 frame
변수 frame에 메모리는 어디에서 할당하나요?
아무리 봐도 frame에 메모리를 할당하는 코드는 없는듯 한데요;
..
[quote]char *frame;[/quote]선언만 하
선언만 하시고 메모리 할당을 안하셨군요.
헉.. 그렇군요...메모리 할당을 안했네요.. :shock: 이
헉.. 그렇군요...
메모리 할당을 안했네요.. :shock:
이런 간단할걸.. 부끄럽습니다..
답변 감사합니다...
댓글 달기