리눅스 C 파일입출력 lstat() 함수 질문이 있습니다.

wone28의 이미지

#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
 
int main()
{
        DIR *dir_info;
        struct dirent *dir;
        struct stat st;
 
        dir_info = opendir(".");
 
        if(dir_info != NULL)
        {
                while(dir = readdir(dir_info))
                {
                        lstat(dir->d_name, &st);
 
                        if(S_ISREG(st.st_mode))
                        {
                                printf("[dir] %s\n", dir->d_name);
                        }
 
                        else if(S_ISDIR(st.st_mode))
                        {
                                printf("[geshifilter-reg] %s\n&quot;, dir-&gt;d_name);&#10;                        }&#10;&#10;                }&#10;        }&#10;        closedir(dir_info);&#10;&#10;        return 0;&#10;}&#10;
ls 명령어와 같이 디렉토리에 있는 파일명을 출력하는 프로그램을 만들었습니다. 위에서 lstat() 이라는 함수를 사용하는데 struct stat *buf 를 인자로 받는것을 보고 &#10;#include &lt;stdio.h&gt;&#10;#include &lt;sys/stat.h&gt;&#10;#include &lt;dirent.h&gt;&#10;#include &lt;unistd.h&gt;&#10;#include &lt;sys/types.h&gt;&#10;&#10;int main()&#10;{&#10;        DIR *dir_info;&#10;        struct dirent *dir;&#10;        struct stat *st;&#10;&#10;        dir_info = opendir(&quot;.&quot;);&#10;&#10;        if(dir_info != NULL)&#10;        {&#10;                while(dir = readdir(dir_info))&#10;                {&#10;                        lstat(dir-&gt;d_name, st);&#10;&#10;                        if(S_ISREG(st-&gt;st_mode))&#10;                        {&#10;                                printf(&quot;[dir] %s\n&quot;, dir-&gt;d_name);&#10;                        }&#10;&#10;                        else if(S_ISDIR(st-&gt;st_mode))&#10;                        {&#10;                                printf(&quot;[reg] %s\n&quot;, dir-&gt;d_name);&#10;                        }&#10;&#10;                }&#10;        }&#10;        closedir(dir_info);&#10;&#10;        return 0;&#10;}&#10; 위와 같이 코드를 수정했는데 세그먼트 오류가 나옵니다. DIR 과 struct dirent 처럼 포인터 변수로 선언하면 왜 오류가 나오는지 궁금합니다. [/geshifilter-reg]
peecky의 이미지

첫 번째 경우는
struct stat st;
라고 변수를 선언하고, 해당 변수의 주소를 lstat() 함수에 넘겨 줬습니다.
lstat() 함수는 해당 주소를 참조해서 결과값을 변수에 채워 넣습니다.

두 번째 경우는 lstat() 함수를 호출할 때 제대로 된 주소값을 주지 않았습니다.
lstat() 함수는 잘못된 주소를 참조해서 결과값을 채워 넣으려다가 오류가 난 겁니다.

wone28의 이미지

포인터에 대한 이해가 부족했네요. 감사합니다

댓글 달기

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