kmm*_*kmm 27 c linux printf portability
在我的程序中,我统计他们想要的文件并发送数据.stat的字段struct
都是特殊类型:
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 */
};
Run Code Online (Sandbox Code Playgroud)
我的问题的相关代码如下:
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);
Run Code Online (Sandbox Code Playgroud)
如何以便携和有效的方式打印这些类型?起初我通过猜测正确的格式说明符而没有强制转换.除了令人讨厌的编程习惯外,这也意味着我的代码无法在32位系统上运行.现在有了演员阵容似乎有效,但有多少平台?
Jon*_*ler 27
没有完全可移植的方式,这是一个令人讨厌的问题.
C99提供了内置类型的机制喜欢size_t
用%zu
符号(也有一些额外的,类似的限定词).
它还提供<inttypes.h>
带有诸如PRIX32之类的宏的头,以定义用于打印32位十六进制常量的正确限定符(在本例中):
printf("32-bit integer: 0x%08" PRIX32 "\n", var_of_type_int32_t);
Run Code Online (Sandbox Code Playgroud)
对于系统定义的类型(例如POSIX定义的类型),AFAIK,没有好办法处理它们.所以,我所做的就是对"安全"转换进行猜测,然后进行相应的打印,包括演员,这就是你在问题中所说明的内容.这令人沮丧,但我知道没有更好的方法.如果有疑问,并使用C99,那么转换为'unsigned long long'是相当不错的; 可能存在使用uintmax_t
强制转换和PRIXMAX或等效的情况.
或者,正如FUZxxl 提醒我的那样,您可以使用修饰符j
来指示"最大"整数类型.例如:
printf("Maximal integer: 0x%08jX\n", (uintmax_t)var_of_type_without_format_letter);
Run Code Online (Sandbox Code Playgroud)