Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以类似“打印”的格式打印文件的详细信息;6月19日10:08“;在Linux下的ubuntu中使用C_C_Linux_File_Ubuntu_Io - Fatal编程技术网

以类似“打印”的格式打印文件的详细信息;6月19日10:08“;在Linux下的ubuntu中使用C

以类似“打印”的格式打印文件的详细信息;6月19日10:08“;在Linux下的ubuntu中使用C,c,linux,file,ubuntu,io,C,Linux,File,Ubuntu,Io,我正试图使用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 {} \; |

我正试图使用
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: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);

}
void displayFileProperties(结构统计*文件,字符*输出属性)
{
输出[缓冲区]的字符部分;
模式=文件->标准模式;
struct tm*时间;
整数月、日、小时、分钟;
//首先注意文件的详细信息,例如权限
//这是一个常规文件
if(S_ISREG(mode))strcat(outputProperties,“-”;
//这是一个管道文件
否则,如果(S_ISFIFO(模式))strcat(输出属性,“p”);
//用于链接文件
否则,如果(S_ISLNK(模式))strcat(输出属性,“l”);
//对于目录文件
否则,如果(S_ISDIR(模式))strcat(输出属性,“d”);
//这是一个套接字文件
否则,如果(S_ISSOCK(模式))strcat(输出属性,“S”);
//用于块设备文件
否则,如果(S_ISBLK(模式))strcat(输出属性,“b”);
//这是一个字符设备文件
否则,如果(S_ISCHR(mode))strcat(outputProperties,“c”);
//权限
(mode&S_IRUSR)?strcat(outputProperties,“r”):strcat(outputProperties,“-”;
(mode&S_IWUSR)?strcat(outputProperties,“w”):strcat(outputProperties,“-”;
(mode&S_IXUSR)?strcat(outputProperties,“x”):strcat(outputProperties,“-”;
//组权限
(mode&S_IRGRP)?strcat(outputProperties,“r”):strcat(outputProperties,“-”;
(mode&S_IWGRP)?strcat(outputProperties,“w”):strcat(outputProperties,“-”;
(mode&S_IXGRP)?strcat(outputProperties,“x”):strcat(outputProperties,“-”;
strcat(outputProperties,“r”):strcat(outputProperties,“-”;
strcat(outputProperties,“w”):strcat(outputProperties,“-”;
strcat(outputProperties,“x”):strcat(outputProperties,“-”;
//打印其他信息
//打印硬链接的数量
sprintf(输出的一部分,“%d”,文件->标准链接);
strcat(输出属性,部分输出);
//打印硬链接的数量
sprintf(部分输出,“%d”,文件->st\u uid);
strcat(输出属性,部分输出);
//打印硬链接的数量
sprintf(部分输出,“%d”,文件->st_gid);
strcat(输出属性,部分输出);
//打印硬链接的数量
sprintf(部分输出,“%d”,(int)文件->st_大小);
strcat(输出属性,部分输出);
//从这里开始注意时间属性
时间=本地时间(&file->st\u mtim);
月=时间->周一+周一;
天=时间->tm\u mday;
小时=时间->标准小时;
最小值=时间->最小值;
sprintf(部分输出,“%d-”,时间->tm_年+1900);
strcat(输出属性,部分输出);
如果(月<10)
strcat(输出属性,“0”);
sprintf(部分输出,“%d-”,月);
strcat(输出属性,部分输出);
如果(第10天)
strcat(输出属性,“0”);
sprintf(部分输出,“%d”,天);
strcat(输出属性,部分输出);
如果(小时<10)
strcat(输出属性,“0”);
sprintf(部分输出,“%d:”,小时);
strcat(输出属性,部分输出);
如果(最小值<10)
strcat(输出属性,“0”);
sprintf(部分输出,“%d”,最小值);
strcat(输出属性,部分输出);
}
我该怎么做?我几乎可以肯定它需要一些小改动,不过我已经搜索过了 但在网上却找不到任何可以澄清这一点的东西

谢谢

strftime()函数对于日期格式非常灵活

strftime()
函数对于日期格式非常灵活


您可以使用strftime:

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

检查一下。它可以用来解决大多数日期和时间格式的问题。

您可以使用
strftime

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

检查一下。它可用于解决大多数日期和时间格式问题。

无需为空终止符(
BUFFER-1
)留出空间,因为
strftime
接受数组长度为
max
(而不是实际字符数)@Nicht Verstehen-我不确定如果没有足够的空空间会发生什么——要么它被忽略了,要么字符串根本没有被写入。你是对的;这会导致字符串根本不被触碰,所以我修改了我的答案。谢谢。无需为空终止符(
BUFFER-1
)留出空间,因为
strftime
接受数组长度为
max
(不是实际字符数)。@Nicht Verstehen-我不确定如果没有足够的空终止符空间会发生什么——要么它被忽略,要么字符串根本没有被写入。你是对的;这会导致字符串根本不被触碰,所以我修改了我的答案。谢谢。谢谢,但它仍然显示相同的输出。我已经添加到您在上述函数末尾提到的行中,但是仍然显示相同的输出。知道哪里出了问题吗?您仍然需要附加
部分输出
strcat(outputProperties,partOfOutput)谢谢,但它仍然显示相同的输出。我已经添加到您在上述函数末尾提到的行中,但是仍然显示相同的输出。知道哪里出了问题吗?您仍然需要附加
部分输出
strcat(outputProperties,partOfOutput)