1

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?

Tas
  • 7,023
  • 3
  • 36
  • 51
  • hi jamek, do we have to use fd and fstat to get a unique identification (etc.. inode id)? is there other function to get it from istream directly? – Arthur George Apr 04 '18 at 10:23
  • Short answer: no. The C++ standard does not specify how iostreams are implemented. inode numbers are an implementation detail on SOME systems, but not others. – Peter Apr 04 '18 at 10:35
  • Did you read? No, there isn't. You have to do it on your own, later if you want to find a file by inode use ftw or similar function - it's depend on your os. https://stackoverflow.com/a/109522/2493292 – jamek Apr 04 '18 at 10:37

0 Answers0