C 静态结构分段故障
我正在使用C中的stat结构,我想输出每个字段。当我尝试输出st_atime、st_mtime和st_ctime时,我使用以下行:C 静态结构分段故障,c,unix,stat,C,Unix,Stat,我正在使用C中的stat结构,我想输出每个字段。当我尝试输出st_atime、st_mtime和st_ctime时,我使用以下行: 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 o
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结构的声明是:
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));
struct stat sb;
#包括
#包括
字符文件[128];
int main(int argc,char*argv[]){
结构统计某人;
sprintf(文件“%s”,argv[1]);
if(stat(file,&sb)==0)
{
printf(“上次更改:%s\n”,ctime(sb.st_-ctime));
printf(“上次文件访问:%s\n”,ctime(sb.st_atime));
printf(“上一个文件模块:%s\n”,ctime(sb.st_mtime));
}
其他的
{
printf(“文件名不存在!\n”);
}
返回0;
}
您应该根据其文档传递对ctime
的引用,因此我建议使用:
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,您应该使用time.h库
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));
或者将声明更改为
printf("Last file change: %s\n", ctime(&(sb.st_ctime)));
你能构造一个吗?你能把包含scruct sb声明和printfs的函数的洞代码放进去吗?它在同一个功能中吗?我已经在原始邮件中包含了它,你没有包括“#include”?哇,这是一个太简单的修复方法。我觉得自己很笨。谢谢,所以muchI尝试了这个,但我似乎仍然有一个分割错误:(
printf("Last file change: %s\n", ctime(&(sb.st_ctime)));
time_t* st_ctime; /* time of last status change */