2016-04-03 10 views
-1
struct stat { 
dev_t  st_dev;  /* ID of device containing file */ 
ino_t  st_ino;  /* inode number */ 
mode_t st_mode; /* protection */ 
nlink_t st_nlink; /* number of hard links */ 
uid_t  st_uid;  /* user ID of owner */ 
gid_t  st_gid;  /* group ID of owner */ 
dev_t  st_rdev; /* device ID (if special file) */ 
off_t  st_size; /* total size, in bytes */ 
blksize_t st_blksize; /* blocksize for file system I/O */ 
blkcnt_t st_blocks; /* number of 512B blocks allocated */ 
time_t st_atime; /* time of last access */ 
time_t st_mtime; /* time of last modification */ 
time_t st_ctime; /* time of last status change */ 
} 

私はstat構造体をC言語で使用していますが、それぞれのフィールドを出力したいと思います。私は、出力のst_atime、st_mtimeの両、およびファイルのst_ctimeしようとすると、次の行を使用しています:何らかの理由でstat構造体分割エラー

printf("Last file change: %s\n", ctime(sb.st_ctime)); 
    printf("Last file access time: %s\n", ctime(sb.st_atime)); 
    printf("Last file mod time: %s\n", ctime(sb.st_mtime)); 

、私はセグメンテーションフォールト(コア・ダンプ)エラーを取得しています。 stat構造体のための私の宣言は次のとおりです。

struct stat sb; 

#include <stdio.h> 
#include <sys/stat.h> 

char file[128]; 

int main(int argc, char *argv[]){ 
struct stat sb; 

sprintf(file, "%s", argv[1]); 


if(stat(file, &sb) == 0) 
{ 
printf("Last change: %s\n", ctime(sb.st_ctime)); 
printf("Last File access: %s\n", ctime(sb.st_atime)); 
printf("Last file mod: %s\n", ctime(sb.st_mtime)); 
    } 
else 
{ 
    printf("File name does not exist!\n"); 
} 

return 0; 

} 
+1

に宣言を変更するには、[最小限のテストケース]を構築することができます(http://stackoverflow.com/help/mcve)? –

+0

scruct sb宣言とprintfsを含む関数を含むホールコードを挿入できますか?それは同じ機能ですか? –

+0

私は元の投稿にそれを含めました – j1nrg

答えて

0

EDIT: をあなたがすべき機能のctimeを使用するにはtime.hライブラリを使用します。

#include <time.h> 

関数ctimeはtime_tへのポインタを受け取ります。

char * ctime(const time_t * timer);

time_t自体を渡しています。構造体のアドレスを渡すか、構造体のtime_t *に時間を変更する必要があります。このアプローチは、構造体をどこに宣言しているかによって危険です。

printf("Last file change: %s\n", ctime(&(sb.st_ctime))); 

または

time_t* st_ctime; /* time of last status change */ 
0

あなたは私が使用することをお勧めそのマニュアルに従ってctimeへの参照を渡す必要があります。

printf("Last file change: %s\n", ctime(&sb.st_ctime)); 
printf("Last file access time: %s\n", ctime(&sb.st_atime)); 
printf("Last file mod time: %s\n", ctime(&sb.st_mtime)); 
+0

私はこれを試したが、まだ私はセグメンテーションフォールトを得るようだ:( – j1nrg