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

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的效果

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

posted @ 2023-10-17 17:17  dctwan  阅读(232)  评论(0编辑  收藏  举报