We can use stat
to get a inode number, which can uniquely identify the object on the system.
code is :
int ret = stat(file_name, &file_info);
if (ret != 0) {
//report error
}
std::cout << file_info.st_ino; inode numb
std::cout = file_info.st_dev;
std::cout = file_info.st_mtime;
So we can check is the file has been moved or renamed. There is another function fstat
which take int fd
, not the filename. Use the filename is not a good solution because the file may be moved after we check it. For example:
stat(file_name)
the inode num is same as before. so the file has not been moved.
-- the file has be moved, but we can not known--//
we open the file
we process new file use old file info
fstat(fd)
the inode num is same as before. so the file has not been moved.
-- the file has be moved
ok. we process the old file use old file info.
Thing goes well, until when I use c++ ifstream
, how can do?
Get a fd
for ifstream
?? or use struct file *file
?
Can everyone help me?