linux系统文件操作函数 (二)

stat、lstat、fstat的区别

函数原型:

int stat(const char *path, struct stat *buf);

int lstat(const char *path, struct stat *buf);

int fstat(int fd, struct stat *buf);

    lstat函数和stat函数作用是类似的,只不过当要读取的文件是符号链接文件时,lstat函数获取的是符号链接文件的属性,而stat函数获取的是符号链接文件指向的实际文件的属性。

#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>


int main(int argc,char*argv[])
{

        if(argc<2)
        {
                printf("need filename");
                exit(1);
        }
        struct stat st;
        int ret = stat(argv[1],&st);//(1)

        int ret = lstat(argv[1],&st);//(2)

        int size = (int)st.st_size;//使用(1)(2)的区别是,当要读取的文件是链接文件时,那么(1)会读取源文件的大小,(2)会读取链接文件的大小。

        printf("file size = %d \n",size);
        return 0;
}

fstat与stat都是取文件的信息,只是函数原型不同。stat的参数可以直接写文件名。而fstat需要先打开文件,然后用文件描述符fd做参数。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值