stat函数用来获取指定路径的文件或者文件夹的信息。

//! 需要包含的头文件
#include <sys/types.h>
#include <sys/stat.h>
//函数原型
int stat(
  const char *filename, //文件或者文件夹的路径
   struct stat *buf //获取的信息保存在内存中
); 正确——返回0 错误——返回-,具体错误码保存在errno中
errno 是记录系统的最后一次错误代码。代码是一个int型的值,在errno.h中定义。查看错误代码errno
是调试程序的一个重要方法。当linux C api函数发生异常时,一般会将errno变量(需include errno.h)
赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因。在实际编程中用这一招解决
了不少原本看来莫名其妙的问题。
//stat结构体是文件(夹)信息的结构体,定义如下:
struct stat
{
dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
ino_t st_ino; /* inode number -inode节点号*/
mode_t st_mode; /* protection -保护模式?*/
nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
uid_t st_uid; /* user ID of owner -user id*/
gid_t st_gid; /* group ID of owner - group id*/
dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
blkcnt_t st_blocks; /* number of 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 - */
};
//例子:使用stat结构图获得文件信息
#include <iostream>  
#include <ctime>   
#include <sys/types.h>    
#include <sys/stat.h>
#include <stdio.h>     
using namespace std;     
int  main ()  
{  
    struct stat buf;    
    int result;    
    result = stat ("./download.c", &buf);    
    if (result != 0)  
      {  
          perror ("Failed ^_^");  
           //cout<<"Failed ^_^";
      }  
    else  
      {    
          //! 文件的大小,字节为单位    
          cout << "size of the file in bytes: " << buf.st_size << endl;    
          //! 文件创建的时间    
          cout << "time of creation of the file: " << ctime (&buf.st_ctime) << endl;   
          //! 最近一次修改的时间    
          cout << "time of last modification of the file: " << ctime (&buf.st_mtime) << endl;    
          //! 最近一次访问的时间    
          cout << "time of last access of the file: " << ctime (&buf.st_atime)<< endl;  
      }    
    return 0;    
lzb@lzb:~/projects/curls/http_post/lib$ ./a.out
size of the file in bytes:
time of creation of the file: Tue Jun :: time of last modification of the file: Tue Jun :: time of last access of the file: Tue Jun ::

上述代码中

C 库函数 perror 把一个描述性错误消息输出到标准错误 stderr。首先输出字符串 str,后跟一个冒号,然后是一个空格。

//函数原型
void perror(const char *str);//str -- 这是 C 字符串,包含了一个自定义消息,将显示在原本的错误消息之前。
#include <stdio.h>

int main ()
{
FILE *fp; /* 首先重命名文件 */
rename("file.txt", "newfile.txt"); /* 现在让我们尝试打开相同的文件 */
fp = fopen("file.txt", "r");
if( fp == NULL ) {
perror("Error ");
return(-);
}
fclose(fp); return();
}

让我们编译并运行上面的程序,这将产生以下结果,因为我们尝试打开一个不存在的文件:

Error : No such file or directory
 

最新文章

  1. Oracle入门基础
  2. 数据库SQL语句练习题
  3. Backbone Backbone-localStorage demo
  4. UIScrollView,UIPageControl
  5. 小谈一下Java I/O
  6. c++ 读取并解析excel文件方法
  7. go build 时报错 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
  8. Leetcode:Repeated DNA Sequences详细题解
  9. [转] vim 正则表达式 很强大
  10. WPF使用资源字典组织资源
  11. Babel运行原理
  12. shell命令总结一
  13. 云服务器ECS优惠券 阿里云 ecs 5折优惠码 阿里云5折优惠码 阿里云5折推荐码 阿里云优惠码 阿里云的5折优惠券 阿里云服务器购买优惠码 服务器购买优惠码
  14. jsp&amp;servlet初体验——用户登录功能实现
  15. 安装.net 服务时出现0x80131515错误的解决办法
  16. 利用java的url实现小型的网页爬虫
  17. nginx实践(五)之代理服务(正向代理与反向代理介绍)
  18. C# Enum,Int,String的互相转换 [转]
  19. Android中使用commons-codec-1.6.jar 进行Base64编解码出现的问题
  20. Paxos共识算法

热门文章

  1. POJ 3983:快算24
  2. 13.56mhz自动寻卡功能业界最低功耗:SI522
  3. hdu 1277 全文检索 (字典树应用)
  4. 一、VIP课程:互联网工程专题 05-快速掌握Jenkins原理与核心功能
  5. 第九篇 AJAX
  6. 第一部分 JavaScript语言核心(四)
  7. Linux重要命令练习之bc
  8. 用Python分析淘宝2000款避孕套,得出这些有趣的结论
  9. WebServerInitializedEvent &amp;ApplicationRunner
  10. 记校赛水题----AK爷兼职计