华清远见上海中心22071班9.1作业

1 . 将stat提取到的文件的属性格式进行整理,贴近为ls -l的格式 

 功能函数

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<time.h>

void modexx(mode_t mode)
{
	int a=0400;
	int flag=0;
	while(a!=0)
	{
		if((mode&a)==0)
		{
			putchar('-');
		}
		else
		{
			switch(flag%3)
			{
				case 0:putchar('r');break;
				case 1:putchar('w');break;
				case 2:putchar('x');break;
			}
		}
		a=a>>1;
		flag++;
	}
}

void time_message(time_t time)
{
	struct tm *p=localtime(&time);
	printf(" %d月 ",(p->tm_mon)+1);
	printf("%2d ",p->tm_mday);
	printf("%02d:%02d",p->tm_hour,p->tm_min);
}

 函数声明

#ifndef __STAT_H__
#define __STAT_H__


void modexx(mode_t mode);

void time_message(int time);


#endif

主函数

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include"stat.h"

int main(int argc, const char *argv[])
{
	struct stat a;
	char *file="02_open.c";
	int f=stat(file,&a);
	if(f<0)
	{
		perror("stat");
		return -1;
	}
	modexx(a.st_mode);
	printf(" %ld",a.st_nlink);
	printf(" %d",a.st_uid);
	printf(" %d",a.st_gid);
	printf(" %6ld",a.st_size);
	time_message(a.st_mtime);
	printf(" %s\n",file);
	return 0;
}

结果 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值