2013-10-04 198 views
4

任何人都知道ino_t類型的佔位符是什麼? 我試圖用printf將它打印出來,並嘗試使用%d,%i,%s和其他但不工作。ino_t的佔位符

printf(" file name = %s, i-node number=**%d**\n", direntp->d_name, direntp->d_ino); 

warning: format ‘%i’ expects argument of type ‘int’, but argument 3 has type ‘__ino_t’ [-Wformat] 

假設我的其他代碼是正確的。 大多數示例僅顯示如何打印名稱,但不顯示inode編號。 我也搜遍了很多地方。

在此先感謝

+0

http://stackoverflow.com/questions/18534336/does-posix-supply-format-string-macros-for-printf-scanf – szx

回答

8

如果你知道類型是不可或缺的,你可以將它轉換爲unsigned long long,並使用%llu

printf(" file name = %s, i-node number=%llu\n", 
     direntp->d_name, (unsigned long long)direntp->d_ino); 
+0

是的。我給了幾乎完全相同的答案。逐詞地。在同一時間。 :) – 2013-10-04 11:00:28

7

記錄:這些被稱爲「轉換說明符」。我知道ino_t沒有轉換說明符。如果您確定它是一個整數,則將其轉換爲unsigned long long並使用"%llu"

+1

當然+1,因爲我沒有很多機會來upvote我自己的答案。 ;-) – jxh

+0

@jxh出於同樣的原因。發生如此罕見:) – 2013-10-04 11:02:17

+0

最安全的方法是使用['stdint.h'](http://pubs.opengroup.org/onlinepubs/000095399/basedefs/stdint.h.html)中的'intmax_t'和'來自['inttypes.h'](http://pubs.opengroup.org/onlinepubs/000095399/basedefs/inttypes.h.html)的PRIdMAX'。 IOW,'printf(「ino =%」PRIdMAX「\ n」,(intmax_t)direntp-> d_ino);'。或者,因爲它可能被認爲是無符號的,'printf(「ino =%」PRIuMAX「\ n」,(uintmax_t)direntp-> d_ino);'。 – glglgl

-1

ino_t是typedef爲無符號長整數。 因此,打印相同的限定符是%lu。

+3

這對所有平臺都適用嗎? –

+0

謝謝,它運作良好。 – Newbie

+1

我相當確定有32位「長」和64位的系統。所以你的答案肯定是錯誤的。 – glglgl