gpt4 book ai didi

c - 在C下的Linux ubuntu中以类似 "Jun 19 10:08"的格式打印文件的详细信息

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:37 25 4
gpt4 key购买 nike

我正在尝试使用 C 显示 Ubuntu 下任何文件的详细信息。

例如,如果我要查找名为 12 的文件/文件夹,我会得到:

a@ubuntu:~/Desktop$ ./exer4 . 12
drwxrwxr-x 3 1000 1000 4096 2012-06-19 10:08
drwxrwxr-x 3 1000 1000 4096 2012-06-16 14:09

但我想这样显示它:

a@ubuntu:~/Desktop$ find . -name 12 -exec ls \-lnd {} \; | sort
drwxrwxr-x 3 1000 1000 4096 Jun 16 14:09
drwxrwxr-x 3 1000 1000 4096 Jun 19 10:08

意思是,我想将这个:2012-06-19 10:08 更改为那个 Jun 19 10:08,或者这个

2012-06-16 14:09Jun 16 14:09

我正在使用以下方法来呈现任何文件的详细信息:

void displayFileProperties(struct stat* file,char*  outputProperties)
{


char partOfOutput[BUFFER];
mode_t mode = file->st_mode;
struct tm* time;
int month, day, hour, min;

// First take care of the file details , e.g permission

// this is a regular file
if (S_ISREG(mode)) strcat(outputProperties,"-");

// the is a pipe file
else if (S_ISFIFO(mode)) strcat(outputProperties,"p");

// for a link file
else if (S_ISLNK(mode)) strcat(outputProperties,"l");

// for a directory file
else if (S_ISDIR(mode)) strcat(outputProperties,"d");

// this is for a socket file
else if (S_ISSOCK(mode)) strcat(outputProperties,"s");

// for a block device file
else if (S_ISBLK(mode)) strcat(outputProperties,"b");

// and this is one for a character device file
else if (S_ISCHR(mode)) strcat(outputProperties,"c");


// Permissions

(mode & S_IRUSR)? strcat(outputProperties,"r"): strcat(outputProperties,"-");
(mode & S_IWUSR)? strcat(outputProperties,"w"): strcat(outputProperties,"-");
(mode & S_IXUSR)? strcat(outputProperties,"x"): strcat(outputProperties,"-");


// Group permission

(mode & S_IRGRP) ?strcat(outputProperties,"r"):strcat(outputProperties,"-");
(mode & S_IWGRP) ?strcat(outputProperties,"w"):strcat(outputProperties,"-");
(mode & S_IXGRP) ?strcat(outputProperties,"x"):strcat(outputProperties,"-");


(mode & S_IROTH)? strcat(outputProperties,"r"):strcat(outputProperties,"-");
(mode & S_IWOTH) ?strcat(outputProperties,"w"):strcat(outputProperties,"-");
(mode & S_IXOTH) ?strcat(outputProperties,"x"):strcat(outputProperties,"-");



//print other information

//print num of hard link
sprintf(partOfOutput," %d ",file->st_nlink);
strcat(outputProperties,partOfOutput);

//print num of hard link
sprintf(partOfOutput,"%d ",file->st_uid);
strcat(outputProperties,partOfOutput);

//print num of hard link
sprintf(partOfOutput,"%d ",file->st_gid);
strcat(outputProperties,partOfOutput);

//print num of hard link
sprintf(partOfOutput,"%d ",(int) file->st_size);
strcat(outputProperties,partOfOutput);


// From here take care of the time properties

time = localtime(&file->st_mtim);

month = time->tm_mon + 1;
day = time->tm_mday;
hour = time->tm_hour;
min = time->tm_min;

sprintf(partOfOutput,"%d-",time->tm_year + 1900);
strcat(outputProperties,partOfOutput);

if(month < 10)
strcat(outputProperties,"0");

sprintf(partOfOutput,"%d-",month);
strcat(outputProperties,partOfOutput);

if(day < 10)
strcat(outputProperties,"0");

sprintf(partOfOutput,"%d ",day);
strcat(outputProperties,partOfOutput);

if(hour < 10)
strcat(outputProperties,"0");

sprintf(partOfOutput,"%d:",hour);
strcat(outputProperties,partOfOutput);

if(min < 10)
strcat(outputProperties,"0");

sprintf(partOfOutput,"%d ",min);
strcat(outputProperties,partOfOutput);

}

我该怎么做?我几乎可以肯定它需要一些小的改动,但是我已经搜索过了网络,但没有找到任何可以澄清这一点的东西。

谢谢

最佳答案

你可以使用strftime:

time = localtime(&file->st_mtim);
strftime(partOfOutput, BUFFER, "%b %d %H:%M", time);

检查 documentation for strftime .它可用于解决日期和时间格式化的大部分问题。

关于c - 在C下的Linux ubuntu中以类似 "Jun 19 10:08"的格式打印文件的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11205961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com