2016-04-03 2 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 Structure Segementation Fault

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)); 

По некоторым причинам, я получаю сообщение об ошибке (дамп ядра) Сегментация Fault. Моя декларация стат структуры является:

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

Можете ли вы поместить код дырки, содержащий функцию, которая содержит декларацию sb и syspfs? это в той же функции? –

+0

Я включил его в исходное сообщение – j1nrg

ответ

0

EDIT: Чтобы использовать функцию CTIME Вам необходимо включить библиотеку time.h, используя

#include <time.h> 

Функция ctime получает указатель на time_t.

char * ctime (const time_t * timer);

Вы сами проходите время_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