首页 > 系统相关 >C++ 获取文件信息(Linux)

C++ 获取文件信息(Linux)

时间:2023-10-17 17:38:56浏览次数:42  
标签:stat struct int C++ st 获取 Linux include buf

stat函数

头文件:#include <sys/stat.h>

int stat(const char* restrict pathname,struct stat* restrict buf);

  • 第一个参数pathname:文件名,需要获取该文件的信息

  • 第二个参数buf:stat函数将pathname对应的文件信息,填入buf指向的stat结构中

  • 返回值:0成功;-1出错

struct stat {
        .......................................................
         ino_t         	st_ino;      		/* inode number*/
         mode_t       	st_mode;     	/* file type & mode */
         nlink_t       	st_nlink;    	/* number of hard links */
         uid_t         	st_uid;      		/* user ID of owner */
         gid_t         	st_gid;      		/* group ID of owner */
         off_t         		st_size;     	/* total size, in bytes */
         unsigned long 	st_blksize;  	/* blocksize  */
         unsigned long 	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 inode last change 
};

基本使用

在当前目录下创建文件file,写入"hello",test.cpp内容如下

#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <iostream>
using namespace std;

int main(){
    struct stat buf;
    int r = stat("file", &buf);
    if(r != -1){
        cout << "inode number = " << buf.st_ino << "\n";
    	cout << "file size = " << buf.st_size << "\n";
    }
    else{
        cout << strerror(errno) << endl;
    }
    return 0;
}
/* output: 
inode number = 164891097
file size = 5*/

相关函数比较

//第一个参数为文件描述符fd
int fstat(int filedes, struct stat *buf);	

//可以识别链接文件,stat直接通过链接定位到原文件
int lstat(const char* restrict pathname,struct stat* restrict buf);		

在当前目录下创建目录mydir,再创建软链接指向mydir

mkdir mydir
ln -s {绝对路径}/mydir youdir

image-20231017164017171

test.cpp内容如下

#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <iostream>

using namespace std;

int main(){
    struct stat buf;
    // stat("file", &buf);
    // cout << "inode number = " << buf.st_ino << "\n";
    // cout << "file size = " << buf.st_size << "\n";
    // int r = stat("yourdir", &buf);   //使用stat  不能识别链接文件
    int r = lstat("yourdir", &buf);     //使用lstat 能够识别链接文件 
    if(r == 0){
        cout << "inode number: " << buf.st_ino << "\n";
        if(S_ISDIR(buf.st_mode)){
            cout << "Is dir\n";
        }
        else{
            cout << "No dir\n";
        }
    }
    else{
        cout << strerror(errno) << endl;
    }
    return 0;
}
/*使用lstat的输出如下
inode number: 164891098
No dir

使用stat的输出如下
inode number: 164891096
Is dir*/

目录操作

头文件:#include <dirent.h>

  • 打开目录

    DIR* opendir(const char* pathname);

    返回值:返回打开目录的索引结构,出错返回NULL

    pathname:要打开的目录名

  • 读取目录项

    struct dirent *readdir(DIR *dp);

    dp:由opendir返回的

    返回值:dp对应的目录中包含的一个目录项

  • 关闭目录

    int closedir(DIR *dp);

    dp:由opendir返回

    返回值:成功返回0,出错返回-1

  • rewinddir函数

    void rewinddir(DIR *dp);

    用来设置目录流目前的读取位置为原来开头的读取位置

dirent结构

struct dirent{
  ino_t d_ino; //索引节点号
  char d_name[NAME_MAX + 1]; //文件名
  //................
}

基本使用:获取目录下所有文件的文件名

#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <iostream>

using namespace std;

int main(){
    DIR *dir;
    struct  dirent  *ptr;   
    dir = opendir("./");
    while((ptr=readdir(dir)) != NULL){
        printf("d_name: %s\n", ptr->d_name);
    }
    int r = closedir(dir);
    if (r == 0){
        cout << "close dir\n";
    }
    else{
        cout << strerror(errno) << "\n";
    }
    return 0;
}

总结

stat函数和目录操作结合起来即可实现ls -l的效果

通过目录操作获取指定目录下所有文件名,然后通过目录路径和文件名获取文件的详细信息

标签:stat,struct,int,C++,st,获取,Linux,include,buf
From: https://www.cnblogs.com/dctwan/p/17770199.html

相关文章

  • 获取本地用户及其所属组
     $Res_Path="d:\temp\"$Time=Get-Date-UFormat"%Y%m%d%H%M"$ComputerName=$env:computername$IP=((gwmiwin32_networkadapterconfiguration|?{$_.DefaultIPGateway-ne$null}).IPAddress)[0]$Res_File_Path=$Res_Path+$Comput......
  • linux定时任务crontab的使用
    linuxcron是不到秒的。crontab参数列表-e#编辑定时任务-l#查看定时任务(其实没用,相当于cat)-r#删除定时任务-u#指定其他用户常用的是crontab-e;表示编辑定时任务。crontab-e和vim/etc/crontab的区别这两种都是编辑定时任务文件。crontab-e相当于cd/var/spool/cro......
  • 使用docker搭建drogon windows10,linux,mac下开发环境
    2023年10月13日14:52:26本机环境Windows10专业版22H2操作内核19045.2965如果直接在windows,linux,mac上直接搭建环境确实有一点难度,之前drogon官方并未提供官方镜像,现在有了docker镜像确实方便了,其实我是最近才有简述安装dockerdesktop,windows的虚拟化有2个方案hyper-v和w......
  • Linux基础——tmpfs挂载到root目录
    1、(卸载/tmp目录前,请备份/tmp目录下重要数据)查看/tmp目录的挂载情况df-h 卸载/tmp目录umount-lf/tmp 2、关闭tmp挂载及tmpfiles相关服务关闭自动挂载/tmp目录服务systemctldisabletmp.mountsystemctlstatustmp.mount 标记masktmp目录服务,禁止对服务操......
  • Qt/C++开源作品45-CPU内存显示控件/和任务管理器一致
    一、前言在很多软件上,会在某个部位显示一个部件,专门显示当前的CPU使用率以及内存占用,方便用户判断当前程序或者当前环境中是否还有剩余的CPU和内存留给程序使用,在不用打开任务管理器或者资源查看器的时候直接得知当前系统的运行情况。尤其是视频监控系统,如果64路全开,肯定很占用CP......
  • c++数组的二进制文件读写
    #include<fstream>//forifstream、ofstreamtemplate<typenameOB>inlinevoidsaveObject(constchar*filename,OB&object,intlength)//传入要保存的对象引用{std::ofstreamosm(filename,std::ios::out|std::ios::binary);osm.write((constcha......
  • linux下自动删除文件夹,如何在Linux中自动删除或清理/tmp文件夹内容?
    https://blog.csdn.net/weixin_36315079/article/details/116614583?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169752720316800211589266%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=1697527203168002115892......
  • linux内核:伙伴算法、slab算法、ptmalloc、tcmalloc使用场景
    linux内核空间Linux内核空间分为三个区域ZONE:ZONE_DMA,ZONE_NORMAL,ZONE_HIGHMEM物理地址空间的顶部以下一段空间,被PCI设备的I/O内存映射占据,它们的大小和布局由PCI规范所决定。640K~1M这段地址空间被BIOS和VGA适配器所占据由于这两段地址空间的存在,导致相应的RAM空间不......
  • Linux 搭建 ftp服务器
    ftp服务器的搭建:文件的上传,文件下载yum-yinstallvsftpd//安装ftpvim/etc/vsftpd/vsftpd.conf   //关闭匿名访问anonymous_enable=NO     //关闭匿名访问systemctlstartvsftpd.service//启动服务systemctlstatusvsftpd.service//查看服务状态如果......
  • linux 防火墙
    netstat是一个控制台命令,可用于监控本机的TCP/IP网络,获得路由表、网络连接以及所有网络接口设备的状态信息netstat-napt  查看监听的端口netstat-napt |grep5672  检查端口被那个进程占用ps6832查看进程的详细信息kill-96832终止进程常用的几个参数有:-a-n......