简体   繁体   English

在linux系统上打印文件的uid

[英]printing uid of a file on linux system

i am learning c programming. 我正在学习c编程。 I am trying to make my own program similar to ls command but with less options.what i am doing is taking input directory/file name as argument and then gets all directory entry with dirent struct(if it is directory) . 我试图使自己的程序类似于ls命令,但选项较少。我正在做的是将输入目录/文件名作为参数,然后获取所有带有dirent结构的目录条目(如果它是目录)。

After it i use stat() to take all information of the file but here is my problem when i use write() to print these value its fine but when i want to print these with printf() i get warninng : format '%ld' expects type 'long int', but argument 2 has type '__uid_t'. 之后我使用stat()来获取文件的所有信息,但是当我使用write()打印这些值时,这是我的问题,但是当我想用printf()打印这些时,我得到warninng:format'%ld '期望类型'long int',但参数2的类型为'__uid_t'。 I don't know what should use at place of %ld and also for other special data types. 我不知道在%ld的地方应该使用什么,也不知道其他特殊数据类型。

There is no format specifier for __uid_t , because this type is system-specific and is not the part of C standard, and hence printf is not aware of it. __uid_t没有格式说明__uid_t ,因为这种类型是系统特定的,不是C标准的一部分,因此printf不知道它。

The usual workaround is to promote it to a type that would fit the entire range of UID values on all the systems you are targeting: 通常的解决方法是将其推广到适合您所定位的所有系统上的整个UID值范围的类型:

printf("%lu\n", (unsigned long int)uid); /* some systems support 32-bit UIDs */

你可以将它强制转换为long int:

printf("foobar: %ld\n", (long int)your_uid_t_variable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM