转换:uid_t 到字符串,off_t 到字符串
我目前正在编写系统编程作业,其中一部分需要获取目录中文件的一些信息。
对于文件的统计信息,我们有 ctime()
函数,它将 time_t
类型转换为 string
并返回指向它的指针。
但是 uid_t
和 off_t
类型又如何呢? 我在网上搜索并没有找到任何功能。或者如果没有任何功能,你能告诉我如何实现这样的功能吗?
I am currently writing a systems programming homework and in one part i need to get some information of a file in a directory.
for the stat of file, we have ctime()
function which converts time_t
type to string
and returns a pointer to it.
but how about the uid_t
and off_t
types? I searched through to internet and couldnt find any function.. Or if there does not exist any function, can you tell me how to implement such a function please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两者都定义为算术类型 (http://www.opengroup .org/onlinepubs/009695399/basedefs/sys/types.h.html),并且在实践中是积极和完整的。 因此,您可以将其转换为 unsigned long long,然后使用 sprintf 和“%llu”来转换为字符串。
Both are defined as arithmetic types (http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html), and in practice are positive and integral. So you can just cast to unsigned long long, and use sprintf with "%llu" to convert to string.
size_t
和off_t
只是无符号整数类型。 (编辑:off_t
很长?看,教训是,检查你的标题!)因此使用
sprintf
(或其他)使用“%i”格式将它们转换说明符。编辑时:糟糕,当我回答时,您将
size_t
更改为uid_t
。uid_t
在types.h
中定义; 看这里。 (它也是一个无符号整型,但是是一个无符号短
。)size_t
andoff_t
are just unsigned integral types. (Edit:off_t
is a long? See, the lesson is, check your headers!)So use
sprintf
(or whatever) to convert them using the "%i" format specifier.On edit: crap, you changed
size_t
touid_t
while I was answering.uid_t
is defined intypes.h
; look there. (It's also an unsigned integral type, but anunsigned short
.)Linux 的 snprintf() 支持
size_t
。 不确定它的可移植性如何,您需要仔细检查“CONFORMS TO”部分。对于
off_t
,您可能需要转换为最大的无符号整数类型,即unsigned long
并使用“lu”说明符。Linux' snprintf() supports the 'z' format specifier for values of type
size_t
. Not sure how portable this is, you'll need to inspect the "CONFORMS TO" section closely.For
off_t
, you might need to cast to the largest unsigned integer type, i.e.unsigned long
and use a "lu" specifier.off_t
是一个long int
:format = "%ld"size_t
是一个unsigned int
:format = "% u"您可以在 sprintf 函数中使用这些格式将其转换为 char*。
off_t
is along int
: format = "%ld"size_t
is anunsigned int
: format = "%u"You can use these format in
sprintf
function to convert into a char*.使用 gcc 5.4.0 以下行:
printf("user #%ld did this or that!\n", uid);
引发以下警告:
..main.c:133:9: warning: format '%ld' Expects argument of type 'long int', but argument 2 has type 'uid_t {aka unsigned int} ' [-Wformat=]
我建议您也这样做并检查编译器的输出。 =)
Using gcc 5.4.0 the following line:
printf("user #%ld did this or that!\n", uid);
raised the following warning:
..main.c:133:9: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘uid_t {aka unsigned int}’ [-Wformat=]
I would suggest you to do the same and check your compiler's output. =)