
stat结构体中很多属性在linux系统下才有效,windows系统下无效
代码语言:javascript代码运行次数:0运行复制<code class="javascript">#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>//包含两个头文件#include<sys/stat.h>#include<sys/types.h>#include<ctime>#include<string.h>//获取文件状态void test01(){//先创建一个结构体变量struct stat mystat;stat("hello.txt", &mystat);printf("文件的大小%d\n", mystat.st_size);//获取atime----最后一次访问时间//ctime返回值是char* char* p = ctime(&mystat.st_atime);printf("%s", p);//因为返回的字符串中包含了换行//去掉换行//方法1://因为指针指向的可能是一个字符串常量,所以强行修改可能会报错char buf[64] = { 0 };strcpy(buf, p);//遇到\0结束拷贝//去掉字符串结尾的\nbuf[strlen(buf) - 1] = '\0';printf("%s", buf);}int main(){test01();system("pause");return 0;}</code>
<code class="javascript">#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>//包含两个头文件#include<sys/stat.h>#include<sys/types.h>#include<ctime>#include<string.h>//获取文件状态void test01(){//先创建一个结构体变量struct stat mystat;stat("hello.txt", &mystat);printf("文件的大小%d\n", mystat.st_size);//获取atime----最后一次访问时间//ctime返回值是char* char* p = ctime(&mystat.st_atime);printf("%s", p);//因为返回的字符串中包含了换行//去掉换行//方法2://因为指针指向的可能是一个字符串常量,所以强行修改可能会报错char buf[64] = { 0 };//拷贝到\n结束strncpy(buf, p, strlen(p)-1);printf("%s", buf);}int main(){test01();system("pause");return 0;}</code>
<code class="javascript">#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>//包含两个头文件#include<sys/stat.h>#include<sys/types.h>#include<ctime>#include<string.h>//获取文件状态void test01(){//先创建一个结构体变量struct stat mystat;stat("hello.txt", &mystat);printf("文件的大小%d\n", mystat.st_size);//获取mtime------最后一次修改时间char* p = ctime(&mystat.st_mtime);char buf[64] = { 0 };strcpy(buf, p);printf("%s", buf);}int main(){test01();system("pause");return 0;}</code>
以上就是获取文件状态的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号