【问题标题】:Printing various FILE info打印各种文件信息
【发布时间】:2019-09-11 04:49:29
【问题描述】:

文件对象是否有各种打印格式化程序?例如:

// pass the filepath
char * filepath = argv[1];
FILE * file = fopen(filepath, "r");
printf("Filepath: %s\n", file.size);

错误信息:

main4.c:416:32: error: no member named 'size' in 'struct __sFILE'

可以从结构中打印的各种项目是什么?

【问题讨论】:

  • this question。另外,如果您想获取文件的大小,请使用ftell
  • @Spikatrix:就 C 标准而言,ftell 无法可靠地获取文件的大小。既然你不能便携,你不妨使用像statfstat这样的非便携函数(或任何Windows等价物)。
  • 该代码不应该产生该错误消息。您的 file 对象是指针,而不是结构。 file->size 而不是 file.size 可能会产生该错误。
  • 这里是可以打印的完整列表:

标签: c


【解决方案1】:

要获取各种信息,您可以使用填充struct statstatlstat

           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 */
           };

man page

man page + example

【讨论】:

  • 如何在 c 程序中使用它?
  • @FI-Info ,请参阅第二个链接的更新答案,其中包含详细示例。
【解决方案2】:

听起来你想使用 stat():

http://man7.org/linux/man-pages/man2/stat.2.html

或者,如果您想定位窗口,则始终有诸如 GetFileSize 之类的 Win32 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-15
    • 2015-05-17
    • 2011-01-21
    • 2014-04-29
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多