Stat 结构分段错误
Stat Structure Segmentation Fault
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 */
}
我正在使用 C 中的 stat 结构,我想输出每个字段。当我尝试输出 st_atime、st_mtime 和 st_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));
出于某种原因,我遇到了分段错误(核心转储)错误。我对 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;
}
您应该根据其文档传递对 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));
编辑:
要使用函数 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 */
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 */
}
我正在使用 C 中的 stat 结构,我想输出每个字段。当我尝试输出 st_atime、st_mtime 和 st_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));
出于某种原因,我遇到了分段错误(核心转储)错误。我对 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;
}
您应该根据其文档传递对 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));
编辑: 要使用函数 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 */