Linxu系统IO函数 stat 和 lstat 函数

stat 和 lstat函数  

作用:获取一个文件相关的一些信息

 st_mode

复制代码
 1 /*
 2     stat: man 2 stat
 3     #include <sys/types.h>
 4     #include <sys/stat.h>
 5     #include <unistd.h>    
 6 
 7     int stat(const char* pathname,struct  stat* statbuf);
 8         作用:获取一个文件相关的一些信息 从路径文件中获取数据传入statbuf中
 9         参数:
10             - pathname:操作的文件的路径
11             - statbuf:结构体变量,传出参数
12         返回值:
13             成功:返回0
14             失败:返回-1 设置errno
15     int lstat(const char* pathname,struct stat* statbuf);
16         作用:获取一个文件相关的一些信息 从路径文件中获取数据传入statbuf中
17         参数:
18             - pathname:操作的文件的路径
19             - statbuf:结构体变量,传出参数
20         返回值:
21             成功:返回0
22             失败:返回-1 设置errno
23 */
24 #include <sys/types.h>  //stat
25 #include <sys/stat.h>   //stat
26 #include <unistd.h>     //stat
27 #include  <stdio.h>
28 
29 int main()
30 {
31     struct stat statbuf;
32     int ret = stat("a.txt",&statbuf);
33     if(ret == -1)
34     {
35         perror("stat");
36         return -1;
37     }
38     printf("size: %ld\n",statbuf.st_size);
39     return 0;
40 }
复制代码

      

此时 vim b.txt文件 会打开 a.txt(类似windows下的快捷方式)   

posted on   廿陆  阅读(25)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示