파일을 한줄단위로 읽어주는 함수에서 Segmentation fault가 납

zkjinz의 이미지

안녕하세요.. 가입하고 처음 올리는글이 질문이라니.. 조금 염치없나요..? 죄송합니다.. ^^;;
다름이 아니라.. 텍스트로 만들어진 애니메이션을 출력해주는 아주 간단한 프로그램을 만들고있는데요..
예전에 만들어 놓은 파일을 한줄단위로 읽는 함수를 그대로 쓰는데.. 어쩐일인지 여기서 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)

theuhm의 이미지

변수 frame에 메모리는 어디에서 할당하나요?
아무리 봐도 frame에 메모리를 할당하는 코드는 없는듯 한데요;

..

익명 사용자의 이미지

Quote:

char *frame;

선언만 하시고 메모리 할당을 안하셨군요.

zkjinz의 이미지

헉.. 그렇군요...
메모리 할당을 안했네요.. :shock:
이런 간단할걸.. 부끄럽습니다..
답변 감사합니다...

댓글 달기

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