The detailed understanding and usage of struct stat structure body

Source: Internet
Author: User

[CPP]View Plaincopy
  1. //! Need to include the de header file
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. int stat (const char *filename, struct stat *buf); //! Prototype, prototype
  5. struct STAT
  6. {
  7. dev_t St_dev; /* ID of device containing file-id*/of the devices on which the file resides
  8. ino_t St_ino; /* Inode number-inode Node number */
  9. mode_t St_mode; /* Protection-protected mode? */
  10. nlink_t St_nlink; /* Number of hard links-how many connections to this file are linked to (rigid connection) */
  11. uid_t St_uid; /* User ID of Owner-user id*/
  12. gid_t St_gid; /* Group ID of Owner-group id*/
  13. dev_t St_rdev; /* Device ID (if special file)-unit number, for device file */
  14. off_t st_size; /* Total size, in bytes-File size, Bytes */
  15. blksize_t st_blksize; /* BlockSize for filesystem I/O-the size of the system block */
  16. blkcnt_t st_blocks; /* Number of blocks allocated-file occupies */
  17. time_t st_atime; /* Time of last access-recent access times */
  18. time_t st_mtime; /* Time of last modification-most recent modified */
  19. time_t st_ctime; /* Time of last status change-*/
  20. };

[CPP]View Plaincopy
  1. #include <iostream>
  2. #include <ctime>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. using namespace Std;
  6. int
  7. Main ()
  8. {
  9. struct stat buf;
  10. int result;
  11. result = Stat ("./makefile", &buf);
  12. if (Result! = 0)
  13. {
  14. Perror ("Failed ^_^");
  15. }
  16. Else
  17. {
  18. //! The size, in bytes, of the file
  19. cout << "Size of the file in bytes:" << buf.st_size << Endl;
  20. //! Time the file was created
  21. cout << "Time of Creation of the file:" << CTime (&buf.st_ctime) <<
  22. Endl
  23. //! Time of last modification
  24. cout << "Time of last modification of the file:" <<
  25. CTime (&buf.st_mtime) << Endl;
  26. //! Time of last visit
  27. cout << "Time of last access of the file:" << CTime (&buf.st_atime)
  28. << Endl;
  29. }
  30. return 0;
  31. }

[CPP]View Plaincopy
    1. $./test
    2. Size of the file in bytes:36
    3. Time of creation of the File:sun 24 18:38:10 2009
    4. Time of last modification of the File:sun 24 18:38:10 2009
    5. Time of last access of the File:sun 24 18:38:13 2009

The detailed understanding and usage of struct stat structure body

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.