#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
int main() {
struct stat file_stat;
if (stat("test.txt", &file_stat) == 0) {
printf("File size: %ld bytes\n", file_stat.st_size);
printf("Last modification time: %s", ctime(&file_stat.st_mtime));
if (S_ISREG(file_stat.st_mode)) {
printf("It is a regular file.\n");
} else if (S_ISDIR(file_stat.st_mode)) {
printf("It is a directory.\n");
}
} else {
perror("stat");
}
return 0;
}
3. utime() 库函数
utime()
函数用于修改文件的访问时间和修改时间。
-
包含头文件:
#include <utime.h>
-
函数声明:
int utime(const char *filename, const struct utimbuf *times);
-
参数说明:
-
filename
:文件名。 -
times
:指向struct utimbuf
结构体的指针,如果为NULL
,则将文件的访问时间和修改时间设置为当前时间。
-
struct utimbuf
结构体定义如下:
struct utimbuf {
time_t actime; // 访问时间
time_t modtime; // 修改时间
};
-
返回值: 成功返回 0,失败返回 -1,并设置
errno
。