如何使用printf显示off_t,nlink_t,为size_t和其他特殊类型? [英] How to use printf to display off_t, nlink_t, size_t and other special types?

查看:1797
本文介绍了如何使用printf显示off_t,nlink_t,为size_t和其他特殊类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的计划,我与stat他们想要的文件,并在发送数据。一个统计结构的字段都为特殊类型:

In my program, I stat the files they want and send the data over. The fields of a stat struct are all special types:

struct stat {
  dev_t     st_dev;     /* ID of device containing file */
  ino_t     st_ino;     /* inode number */
  mode_t    st_mode;    /* protection */
  nlink_t   st_nlink;   /* number of hard links */
  uid_t     st_uid;     /* user ID of owner */
  gid_t     st_gid;     /* group ID of owner */
  dev_t     st_rdev;    /* device ID (if special file) */
  off_t     st_size;    /* total size, in bytes */
  blksize_t st_blksize; /* blocksize for file system I/O */
  blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
  time_t    st_atime;   /* time of last access */
  time_t    st_mtime;   /* time of last modification */
  time_t    st_ctime;   /* time of last status change */
};

相关code我的问题如下:

The relevant code for my question follows:

len = snprintf( statbuf, STAT_BUFFER_SIZE,
  "%crwxrwxrwx %lu %u %u %lld %s %s\r\n",
  S_ISDIR( filestats.st_mode ) ? 'd' : '-',
  (unsigned long ) filestats.st_nlink,
  filestats.st_uid,
  filestats.st_gid,
  (unsigned long long ) filestats.st_size,
  date,
  filename);

如何打印这些类型的便携式和有效的方式?起初我通过猜测正确的格式说明这样做是没有转换。除了是一个恼人的编程习惯,这也意味着我的code将不会在32位系统上工作。现在它似乎工作的演员,但多少平台?

How do I print these types in a portable and efficient way? At first I did it without casts by guessing the correct format specifiers. Apart from being an annoying programming habit, this also meant my code wouldn't work on a 32 bit system. Now with the casts it seems to work, but on how many platforms?

推荐答案

有没有做一个完全可移植的方式,它是一个麻烦。

There isn't a fully portable way to do it, and it is a nuisance.

C99提供了一种机制内置的类型,如为size_t %祖符号(也有一些额外的,类似的限定符)。

C99 provides a mechanism for built-in types like size_t with the %zu notation (and there are some extra, similar qualifiers).

它还提供了< inttypes.h> 与宏如PRIX32定义正确预选赛打印32位十六进制常量(在这种情况下)头

It also provides the <inttypes.h> header with macros such as PRIX32 to define the correct qualifier for printing a 32-bit hexadecimal constant (in this case):

printf("32-bit integer: 0x%08" PRIX32 "\n", var_of_type_int32_t);

有关系统定义的类型(如那些由POSIX定义),据我所知,有来处理它们没有什么好办法。所以,我做的是采取飞行的猜测在安全的转换,然后打印因此,包括中投,这就是你的问题说明。这是令人沮丧的,但有没有更好的办法,我知道的。如有疑问,并使用C99,然后转换为无符号长长'是pretty好;有可能是使用强制转换为 uintmax_t型和PRIXMAX或等同的情况。

For the system-defined types (such as those defined by POSIX), AFAIK, there is no good way to handle them. So, what I do is take a flying guess at a 'safe' conversion and then print accordingly, including the cast, which is what you illustrate in the question. It is frustrating, but there is no better way that I know of. In case of doubt, and using C99, then conversion to 'unsigned long long' is pretty good; there could be a case for using a cast to uintmax_t and PRIXMAX or equivalent.

或者像 FUZxxl <一个href=\"http://stackoverflow.com/questions/1401526/how-to-use-printf-to-display-off-t-nlink-t-size-t-and-other-special-types/1401581?noredirect=1#comment48743557_1401581\">reminded我说,你可以使用修改Ĵ来表示,最大整数类型。例如:

Or, as FUZxxl reminded me, you can use the modifier j to indicate a 'max' integer type. For example:

printf("Maximal integer: 0x%08jX\n", (uintmax_t)var_of_type_without_format_letter);

这篇关于如何使用printf显示off_t,nlink_t,为size_t和其他特殊类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆