前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >获取文件状态

获取文件状态

作者头像
大忽悠爱学习
发布2021-03-02 15:09:07
1.5K0
发布2021-03-02 15:09:07
举报
文章被收录于专栏:c++与qt学习
在这里插入图片描述
在这里插入图片描述

stat结构体中很多属性在linux系统下才有效,windows系统下无效

代码语言: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结束拷贝
	//去掉字符串结尾的\n
	buf[strlen(buf) - 1] = '\0';
	printf("%s", buf);
}
int main()
{
	test01();
	system("pause");
	return 0;
}
在这里插入图片描述
在这里插入图片描述
代码语言: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;
}
在这里插入图片描述
在这里插入图片描述
代码语言: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;
}
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/02/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档