[완료]리눅스 공부하는 학생입니다. 제가 짠 소스가 안되는 이유가 궁금합니다.

obshaha의 이미지

간단한건데.. 제대로 못하네요.. ㅡㅡ;
제 생각엔 안되는 이유를 잘 모르겠어서요.. ;;
짠거는 입력한 파일에 대해 접근시간을 이용해 변경되었는지 확인하는겁니다.
파일 열고 다시 저장하고 해도.. 계속 아직 변경되지 않았다는 메시지만 출력되네요.. ;;

#include
#include
#include
#include
#include

main()
{
char buffer[]={};
struct stat finfo;
time_t last1;

printf("내용이 변경되었는지 확인하려는 파일명을 입력하세요 : ");
scanf("%s", buffer);
printf("당신이 검사할 파일은 %s입니다.\n", buffer);

stat(buffer, &finfo);
last1=finfo.st_atime;

while(1)
{
stat(buffer, &finfo);
if(last1!=finfo.st_atime)
printf("파일이 변경되었습니다.\n");
else
{
printf("파일이 아직 변경되지 않았습니다.\n");
sleep(2);
}
}
}

그리고 한가지 더 입니다. ㅡㅡ;
이건.. 프롬프트창에서 ls 명령어 치면.. 이름하고 모 간단한 내용 나오잖아요? -l 옵션 주면
더 상세히 나오고.. ㅡㅡ; 대충 따라해본다고 했는데 잘 안되네요.. 에휴~
밑에 파일하고 디렉토리 구분하는 곳에서 세그먼트 오류가 납니다. ㅠ.ㅠ
정말 모르겠습니다.;;;;

#include
#include
#include
#include
#include
#include
#include
#include

main(int argc, char *argv[])
{
DIR *dirp;
struct dirent *dentry;
struct stat finfo;
int count=0;

if((dirp=opendir(".")) == NULL)
{
printf("디렉토리 열기 실패\n");
exit(1);
}

if(argc!=2)
{
while((dentry=readdir(dirp)) != NULL)
{
printf("%s ", dentry->d_name);
count++;
if(count==4)
printf("\n");
}
}
else
{
while((dentry=readdir(dirp)) != NULL)
{
stat(dentry->d_name, &finfo);
if(S_ISDIR(finfo.st_mode))
//if((stat(dentry->d_name, &finfo)) == -1)
{
printf("디렉터리입니다. ");
printf("%s ", dentry->d_name);
printf("%s ", dentry->d_ino);
}
else
{
printf("파일입니다. ");
stat(dentry->d_name, &finfo);
printf("%o ", finfo.st_mode);
printf("%d ", finfo.st_nlink);
printf("%d ", finfo.st_uid);
printf("%d ", finfo.st_gid);
printf("%d ", finfo.st_size);
printf("%u ", finfo.st_atim);
printf("%s\n", dentry->d_name);
}
}
}
closedir(dirp);
}

여기는 얼마전에 알았는데 고수분들이 굉장히 많은것 같아서요.. ;;
스스로 생각해도 참.. 지금 허접해서 부끄러운데.. 공부는 해보려고 합니다.
제발 도와주세요.. ;; 정말 감사하겠습니다.

karkayan의 이미지

첫번째는 buffer에 공간이 할당되어 있지 않네요.
char buffer[] 대신에 char buffer[1024] 식으로 충분한 공간을 주면 해결됩니다.
while 문 안에 있는 stat 함수 앞에 buffer의 내용을 출력해 보면 뭐가 문제인지 알 수 있을 겁니다.

두번째는 "디렉토리입니다"를 출력하고 아래쪽에 d_ino을 %s로 출력하는게 문제네요.
이런 세그멘테이션 폴트 같은 건 gdb를 이용하시면 보다 쉽게 어디가 문제인지를 확인할 수 있습니다.

obshaha의 이미지

아이노드 번호때문에 읽다 실패한거 고쳤습니다. ㅋ
근데 죄송한데 위에꺼는 배열 크기 지정해줘도 안되네요..
음.. stat 함수를 똑같은거 두번써서 그런가요.. 파일명이 같은거라.. ;;
그건 아닌것 같고.. 다시 읽어들이는거랑은 상관없지 않나요? 근데 왜 안되지.. ㅠ.ㅠ

그래도 답변 감사하고.. 정말 고맙습니다. 큰 도움이 되었어요 ^^

ddoman의 이미지

전 됩니다.
아래는 제가 다시 작성한 코드니 한번 컴파일 해서 돌려보세요.
실행방법은..

gcc -o check filename.c
./check
(파일이름은 filename.c로 입력)

해서 실행 파일을 돌려보신후,
다른 터미널 창을 열으셔서,

touch filename.c

해보세요.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
 
int main( int argc, char* argv[] )
{
        char buffer[1024];
        struct stat finfo;
        time_t last1;
 
        printf("내용이 변경되었는지 확인하려는 파일명을 입력하세요 : ");
        scanf("%s", buffer);
        printf("당신이 검사할 파일은 %s입니다.\n", buffer);
 
        stat(buffer, &finfo);
        last1=finfo.st_atime;
 
        while(1)
        {
                if( stat(buffer, &finfo) == -1 )
                        perror( "stat" );
                printf( "Last = %ld, Now = %ld", last1, finfo.st_atime );
                if(last1!=finfo.st_atime)
                {
                        printf("파일이 변경되었습니다.\n");
                        break;
                }
 
                else
                {
                        printf("파일이 아직 변경되지 않았습니다.\n");
                        sleep(2);
                }
        }
}
obshaha의 이미지

이 코드 잘되네요.. 음.. 위에꺼랑 다른게 모지..
if로 검사해준것밖에 다른게 없는것 같은데..
여튼 감사해요. 성심껏 답 달아주시고.. ^^
정말 감사드립니다. 수고많으셨어요.

댓글 달기

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