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
 

struct stat的更多相关文章

  1. 使用struct处理二进制

    有的时候需要用python处理二进制数据,比如,存取文件.socket操作时.这时候,可以使用python的struct模块来完成. struct模块中最重要的三个函数是pack(), unpack( ...

  2. golang struct扩展函数参数命名警告

    今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...

  3. go-使用 unsafe 修改 struct 中的 field 的值

    以下是方法,不要纠结原理,等东西积累多了,你才有能力纠结原理: 首先,你需要有一个这样的函数,这是在 nsq 的源码里直接抄过来的: func unsafeValueOf(val reflect.Va ...

  4. C语言中struct位域的定义和使用

    位域的定义和使用 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又 ...

  5. C# Struct结构体里数组长度的指定

    typedef struct Point{ unsigned short x; unsigned short y; }mPoint;//点坐标 typedef struct Line{ mPoint ...

  6. C 语言Struct 实现运行类型识别 RTTI

    通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型.    (2)dynamic_cast: ...

  7. VC++ : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<wchar_t,struct std::char_traits<wchar_t>

    最近学习Google Breakpad,将其用在了自己的项目中,编译的版本为VS2010,没有什么问题.但是为了和之前的程序兼容,需要使用VS2008版本的程序,于是又编译了VS2008版本的代码,但 ...

  8. 字节流与数据类型的相互转换---使用struct模块

    字节流与数据类型的相互转换---使用struct模块 http://blog.csdn.net/Sunboy_2050/article/details/5974029 Python是一门非常简洁的语言 ...

  9. 窥探Swift之别具一格的Struct和Class

    说到结构体和类,还是那句话,只要是接触过编程的小伙伴们对这两者并不陌生.但在Swift中的Struct和Class也有着令人眼前一亮的特性.Struct的功能变得更为强大,Class变的更为灵活.St ...

  10. struct 大小计算

    结构体是一种复合数据类型,通常编译器会自动的进行其成员变量的对齐,已提高数据存取的效率.在默认情况下,编译器为结构体的成员按照自然对齐(natural alignment)条方式分配存储空间,各个成员 ...

随机推荐

  1. 批量处理文件的Python程序

    经常批量处理文件,这里有个python的模板,保存一下 这个例子是把目录里面所有子目录的mp3文件放慢0.85倍并保存到./processed/目录下面. #coding=utf-8 import s ...

  2. JS - 使 canvas 背景透明

    canvas = document.getElementById('canvas1'); var context = canvas.getContext('2d');context.fillStyle ...

  3. nodeks —— fs模块 —— 从流中 读取和写入数据

    Fs流读取和写入数据 使用文件流来读取大文件不会卡顿 1, 从流中读取数据 var fs = require("fs"); var data = ''; var count = 0 ...

  4. 10 分钟彻底理解 Redis 的持久化和主从复制

    在这篇文章,我们继续有关Redis方面知识的学习,一起了解一下其中一个非常重要的内容:Redis的持久化机制. 什么是Redis持久化? Redis作为一个键值对内存数据库(NoSQL),数据都存储在 ...

  5. 读取docx表格中的信息

    参考了 http://blog.csdn.net/qq_34475777/article/details/62055523 http://www.cnblogs.com/deepwaterplan/a ...

  6. css选择器权重、样式继承、默认样式

    学过css的小伙伴都是指css选择器的权重 !important Infinity 行间样式 1000 id   100 class|属性|伪类 10 标签|伪元素 1 通配符 0 权重相同 相同cs ...

  7. 七、JavaScript之console.log输出和document.write输出

    一.代码如下 二.运行效果如下 三.点击之后,效果如下 四.按一下F12,在控制台中可以看到

  8. 046-unset对静态变量无法销毁

    <?php function digui() { static $count = 0; echo $count; $count++; unset($count); } digui(); digu ...

  9. 笔记本如何不按Fn键就能实现F键的功能

    笔记本的F1~F12键的附带功能如何改成 不用按Fn键就能实现F1~F12的功能 本人现在使用的是一款ThinkPad的本本,之前在台式机上愉快的玩耍的时候键盘上的F键直接按一下就可以实现相应的功能, ...

  10. mvvm双向绑定机制的原理和代码实现

    mvvm框架的双向绑定,即当对象改变时,自动改变相关的dom元素的值,反之,当dom元素改变时,能自动更新对象的值,当然dom元素一般是指可输出的input元素. 1. 首先实现单向绑定,在指定对象的 ...