开发者

How to determine when a file is updated in c++?

I am using an application which takes File X as input and gives File Y and Z as output (graphs and plots)

Now if the applicatoin is run again :

  1. File Y and Z already exists and File X is not updated then it should not be over-written.
  2. File X is updated, then File Y and Z must be overwritten.

The code is written in c++, how can i change the code such 开发者_如何学Pythonthat both the conditions are met? Should I take time stamp of X everytime?


If you are on a POSIX machine, use stat or a related function such as fstat. The windows equivalents are described at http://msdn.microsoft.com/en-us/library/14h5k7ff(v=vs.80).aspx.

The structure returned by stat or the windows counterparts have a field that represents the time at which the file was last modified.


For a portable solution, Boost.Filesystem has last_write_time that returns the time of last modification as a std::time_t.


You could just keep a hash of file X. If the has changes, the file has been changed (updated), and you will need to update Y and Z.

if (XHashPrev != XHashNow || !YZExist)
    // update files.


using David Hammen's answer, compare modification timestamp of X against Y and Z, if X was modified later than Y or Z - so they are outdated and should be rewritten


Yes, you should inspect the file X and see (two options):

  • if its update date changed or
  • or if its content changed (using a CRC32 as an example).

The first is the easiest and the fastest. The second is the most accurate.
EDIT further to the comments below: These two options can be combined to reach a compromise.


Another possibility: write the basic program with X always overwriting Y and Z. Invoke the basic program with make. A makefile that looks like:

Y Z : X
    basicProgram X Y Z

should do the trick. You can then invoke it with either "make Y" or "make Z" Bottom line: let make worry about timestamps and such.


Please note that when checking for last update time, there are some limitations:

... The [time] resolution is as low as one hour on some filesystems... During program execution, the system clock may be set to a new value by some other, possibly automatic, process ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜