Memory fault(coredump) 가 계속 떨어지네요... 왜 이러죠???

skyphase의 이미지

VC++만 하다가 UNIX-C 하려고 하니...
적응하는데 좀 시간이 걸리네요... ^^;;;

아래 프로그램은...
첫번째 인자를 디렉토리 경로를 넣어주면...
그 경로에 해당하는 파일 목록을 시간순으로 정렬하여...
_FileList.dat 이라는 파일을 생성합니다. (GetFileList 함수의 역할)

그리고 ExcuteCompensate 함수에서는...
생성된 _FileList.dat 파일을 열어서...
시간순으로 정렬된 파일을 하나 하나 읽어다가 일을 처리하고자 합니다.

그런데... 윈도우에서 VC++ 할때는 전혀 아무런 오류도 없는데...
유닉스에서 하면 코어덤프 오류가 딱 뜨네요... ㅡㅡ;;;
뭐... 윈도우에서는 ls -al 해서 파일로 만드는걸 하지 못하니 당연한 걸수도... ㅡㅡ;;;

아무래도 ls -al >> _FileList.dat 작업을 진행후에도 파일을 놓아주지 않고 있는것 같네요...
방법이 없을까요??? ㅡㅡ;;;

#define _JHFILE_LIST "_FileList.dat"

void GetFileList(char *cpInPath)
{
FILE *pWriteFile = NULL;
pWriteFile = fopen(_JHFILE_LIST, "w");
fclose(pWriteFile);

char *ptCommand = (char*)malloc( strlen(cpInPath) + 30 );
memset( ptCommand, 0x00, strlen(cpInPath) + 30 );

strcpy(ptCommand, "ls -al ");
strncat(ptCommand, cpInPath, strlen(cpInPath));
strncat(ptCommand, " | sort +rn +6 +7 >> ", strlen(" | sort +rn +6 +7 >> "));
strncat(ptCommand, _JHFILE_LIST, strlen(_JHFILE_LIST));
system(ptCommand);

printf("%s\n", ptCommand);
}

void ExcuteCompensate()
{
FILE *pReadFile = NULL ;
if ( (pReadFile = fopen(_JHFILE_LIST,"r+")) == NULL )
{
memset(g_szMessage, 0x00, sizeof(g_szMessage));
sprintf(g_szMessage, "\n▷ ExcuteCompensate Function :\n Couldn't open file %s; %s\n\n", _JHFILE_LIST, strerror(errno));
printf(g_szMessage);
}

char ch;
int nLength = 0;
char cBuffer[1024];
char cTemp[1024];
memset(cBuffer, 0x00, sizeof(cBuffer));

while( 1 )
{
ch = fgetc(pReadFile); /* 여기서 코어덤프 오류 발생 */


if ( feof(pReadFile) )
break;

cBuffer[nLength] = ch;
nLength++;

if ( ch == 0x0A )
{
memset(cTemp, 0x00, sizeof(cTemp));
strncpy( cTemp, &cBuffer[44], strlen(cBuffer) );
printf("%s", cBuffer);

nLength = 0;
memset(cBuffer, 0x00, sizeof(cBuffer));
}
}
}

void main(int argc, char* argv[])
{
GetFileList(argv[1]);
ExcuteCompensate();
}

litdream의 이미지

indent 를 유지해서 올려주시면 도움이 되겠습니다. :)

제가 돌려보지는 않았는데,
system() 에 붙일 명령 만드실때, sort 스위치가 잘못 된듯 합니다.
참고하세요.

% ls /tmp | sort +rn +6 +7
sort: open failed: +rn: No such file or directory

그리고, 커맨드 만드실때, ls -al 로 찾으시는데, 굳이 sort 를 하실요량이시면,
man ls 해서 필요한 옵션을 한번 찾아보세요. 나름대로 ls 에 왠만큼 필요한 sort 는
다 있을겁니다. 이름으로 sort 하실것 같으면 그냥 ls 만 하셔도 기본적으로 이름으로
소트가 되어 있습니다. (case sensitive 라서 그렇지만서도..)

아 그리고, GetFileList() 에 memory leak 이 있군요 :) free 잊지말고 해주신후 return 해주세요.

삽질의 대마왕...

삽질의 대마왕...

litdream의 이미지

indent 를 유지해서 올려주시면 도움이 되겠습니다. :)

제가 돌려보지는 않았는데,
system() 에 붙일 명령 만드실때, sort 스위치가 잘못 된듯 합니다.
참고하세요.

% ls /tmp | sort +rn +6 +7
sort: open failed: +rn: No such file or directory

그리고, 커맨드 만드실때, ls -al 로 찾으시는데, 굳이 sort 를 하실요량이시면,
man ls 해서 필요한 옵션을 한번 찾아보세요. 나름대로 ls 에 왠만큼 필요한 sort 는
다 있을겁니다. 이름으로 sort 하실것 같으면 그냥 ls 만 하셔도 기본적으로 이름으로
소트가 되어 있습니다. (case sensitive 라서 그렇지만서도..)

아 그리고, GetFileList() 에 memory leak 이 있군요 :) free 잊지말고 해주신후 return 해주세요.

삽질의 대마왕...

삽질의 대마왕...

litdream의 이미지

어, 저는 한번밖에 안보냈는데, 글이 두개가 올라갔네요.
그리고, 제가 지울수 있는 권한이 없네요?
혹시 권한 있으신분 보시면 하나 없애주시고, 이글도 없애주세요.

삽질의 대마왕...

삽질의 대마왕...

ymir의 이미지

sort 옵션을 보니.. Windows sort 명령인것 같군요...
*nix 에서는 에러가 나서 파일이 생성되지 않았을 겁니다..
그런데 ExcuteCompensate() 함수에서는..
fopen() 에러체크 후에 리턴안하고 계속 진행하는군요..
(아마 여기서 file open 에러가 나서 file pointer 가 NULL 일것 같은데...)

참고로.. 위에서 말씀하신대로 굳이 sort 명령 안써도
ls 에 sort 옵션이 다 있습니다..

이건 사족인데...
가급적 alloc/free, open/close 정도는 맞춰주시는게 좋습니다.. ;;;

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

mg2000의 이미지

글의 삭제는 편집 탭을 클릭하시면 지우기 버튼이 나옵니다.

코드는 code태그를 이용하시면 이쁘게 나오고요

익명 사용자의 이미지

ptCommand에 저장되는 문자열 길이가 strlen(cpInPath) + 30 을 넘겠는데요.
윈도우즈에서 에러없다는 게 더 이상하군요.

sandy의 이미지

..음 pReadFile 의 내용을 gdb에서 확인하세요.

..

댓글 달기

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