C++ method to obtain file status information

  • 2020-04-02 03:09:43
  • OfStack

This article illustrates a C++ method for obtaining file status information. Share with you for your reference. The details are as follows:


//C++ to get file status information source,
//C++ to get the file's disk source code,
//C++ file creation time source,
//C++ access time source code,
//C++ last modified date source code, No such file or directory
#include<iostream.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
void main( void )
{
  struct stat buf;
  int result;
  //Get file status information
  result =stat( "D:ok2002.txt", &buf );
  //Displays file status information
  if( result != 0 )
    perror( "Displays file status information error " );//And indicates the reason for the error, such as No such file or directory
  else
  {
    cout<<" The file size :"<<buf.st_size<<" byte "<<endl;
    cout<<" The disk character  :";
    cout<<char(buf.st_dev + 'A')<<endl;
    cout<<" File creation time :"<<ctime(&buf.st_ctime);
    cout<<" Visit date :"<<ctime(&buf.st_atime);//Note here that the access time of 00:00:00 is normal
    cout<<" Last modified date :"<<ctime(&buf.st_mtime);
  }
}
//Please open your D disk, modify the ok2002.txt to your existing D disk file, or create a new ok2002.txt, and then run the above code
//Compare the results to open your D disk on ok2002.txt, right-click/select properties, popup window on the information, information is to

Hope that the article described in the C++ programming to help you.


Related articles: