Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检测文件是否重写?,_C++_C_File_Io - Fatal编程技术网

C++ 如何检测文件是否重写?,

C++ 如何检测文件是否重写?,,c++,c,file,io,C++,C,File,Io,在我的C/C++程序中,我需要检查我读取的文件是否被重写(其inode已更改或添加了一些新行)。如果我现在错了,fstat和fstat64只能在我使用Linux而不是windows时使用。有没有通用的(用于复杂操作系统的)方法可以做到这一点?另外,您能告诉我如何使用fstat64实现这一点吗?我没有代码示例,但您能对照您第一次打开文件时的时间检查文件的上次修改时间吗 编辑 找到了一个很好的代码片段,似乎可以实现这个目的 我没有代码示例,但是您可以对照您第一次打开文件时的时间检查文件的上次修改时间

在我的C/C++程序中,我需要检查我读取的文件是否被重写(其inode已更改或添加了一些新行)。如果我现在错了,
fstat
fstat64
只能在我使用Linux而不是windows时使用。有没有通用的(用于复杂操作系统的)方法可以做到这一点?另外,您能告诉我如何使用fstat64实现这一点吗?

我没有代码示例,但您能对照您第一次打开文件时的时间检查文件的上次修改时间吗

编辑 找到了一个很好的代码片段,似乎可以实现这个目的


我没有代码示例,但是您可以对照您第一次打开文件时的时间检查文件的上次修改时间吗

编辑 找到了一个很好的代码片段,似乎可以实现这个目的


您可以跟踪上次写入文件的时间,以了解文件是否已被修改。跨平台解决方案使用boost::filesystem。Windows没有fstat64 AFAIK

http://rosettacode.org/wiki/File_modification_time#C.2B.2B
#包括
#包括
#包括
int main(int argc,char*argv[]){
如果(argc!=2){

std::cerr您可以跟踪文件上次写入的时间,以了解文件是否已被修改。跨平台解决方案使用boost::filesystem。Windows没有fstat64 AFAIK

http://rosettacode.org/wiki/File_modification_time#C.2B.2B
#包括
#包括
#包括
int main(int argc,char*argv[]){
如果(argc!=2){

STR:文件操作不是C++级的函数。我认为你不会发现任何通用的,特别是对于你想要的函数。你可以生成文件的哈希。要检查它是否已经改变了你,那么重新对文件进行哈希和比较。可能不是你想要的。但是文件系统状态的操作依赖于文件系统。正在使用em(这甚至不是特定于操作系统的,因为某些操作系统可以使用多个不同的文件系统)文件操作不是C++级函数。我认为您不会发现任何通用的,特别是对于您想要的函数。您可以生成文件的哈希。要检查它是否已经更改了,然后重新对文件进行哈希和比较。可能不是您想要的。但是文件系统状态的操作依赖于文件系统。已使用(这甚至不是特定于操作系统的,因为某些操作系统可以使用多个不同的文件系统)。
http://rosettacode.org/wiki/File_modification_time#C.2B.2B

#include <boost/filesystem/operations.hpp>
#include <ctime>
#include <iostream>

int main( int argc , char *argv[ ] ) {
   if ( argc != 2 ) {
      std::cerr << "Error! Syntax: moditime <filename>!\n" ;
      return 1 ;
   }
   boost::filesystem::path p( argv[ 1 ] ) ;
   if ( boost::filesystem::exists( p ) ) {
      std::time_t t = boost::filesystem::last_write_time( p ) ;
      std::cout << "On " << std::ctime( &t ) << " the file " << argv[ 1 ] 
     << " was modified the last time!\n" ;
      std::cout << "Setting the modification time to now:\n" ;
      std::time_t n = std::time( 0 ) ;
      boost::filesystem::last_write_time( p , n ) ; 
      t = boost::filesystem::last_write_time( p ) ;
      std::cout << "Now the modification time is " << std::ctime( &t ) << std::endl ;
      return 0 ;
  } else {
      std::cout << "Could not find file " << argv[ 1 ] << '\n' ;
      return 2 ;
  }