find inode number of a file using C code

耗尽温柔 提交于 2019-12-05 06:58:54

You can use file descriptor to get the inode number, use this code :

int fd, inode;  
fd = open("/path/to/your/file", YOUR_DESIRED_OPEN_MODE);

if (fd < 0) {  
    // some error occurred while opening the file  
    // use [perror("Error opening the file");] to get error description
}  

struct stat file_stat;  
int ret;  
ret = fstat (fd, &file_stat);  
if (ret < 0) {  
   // error getting file stat  
} 

inode = file_stat.st_ino;  // inode now contains inode number of the file with descriptor fd  

// Use your inode number
// ...

fstat is a system call that is used to determine information about a file based on its file descriptor. It is described here

stat is a structure that contains meta information of a file and is described here
to use stat structure and fstat system call you should add #include <sys/stat.h> to your code.

You can find the inode number of a file using stat: http://linux.die.net/man/2/stat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!