转换:uid_t 到字符串,off_t 到字符串

发布于 2024-07-18 02:07:41 字数 256 浏览 6 评论 0原文

我目前正在编写系统编程作业,其中一部分需要获取目录中文件的一些信息。

对于文件的统计信息,我们有 ctime() 函数,它将 time_t 类型转换为 string 并返回指向它的指针。

但是 uid_toff_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

嘿嘿嘿 2024-07-25 02:07:41

两者都定义为算术类型 (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.

无可置疑 2024-07-25 02:07:41

size_toff_t 只是无符号整数类型。 (编辑:off_t 很长?看,教训是,检查你的标题!)

因此使用 sprintf (或其他)使用“%i”格式将它们转换说明符。

编辑时:糟糕,当我回答时,您将 size_t 更改为 uid_tuid_ttypes.h中定义; 看这里。 (它也是一个无符号整型,但是是一个无符号短。)

size_t and off_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 to uid_t while I was answering. uid_t is defined in types.h; look there. (It's also an unsigned integral type, but an unsigned short.)

锦欢 2024-07-25 02:07:41

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.

待"谢繁草 2024-07-25 02:07:41

off_t 是一个 long int:format = "%ld"

size_t 是一个 unsigned int:format = "% u"

您可以在 sprintf 函数中使用这些格式将其转换为 char*。

off_t is a long int: format = "%ld"

size_t is an unsigned int: format = "%u"

You can use these format in sprintf function to convert into a char*.

执妄 2024-07-25 02:07:41

使用 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. =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文