开发者

In C++, I want to make a loop that constantly checks the size of a file and do something if the size changes

I'm trying to make a program that will constantly check the size of a file and if the size of the file changes, make the program do something. The file is an html file so another method could be to constantly check the source of it and if the number of characters chan开发者_运维百科ges, do something.

I think I could do that without much trouble but the thing I'm having a problem with is having something constantly check the file. Is there a function that can do that? I'd like it to check every 10 seconds.

edit: The operating system is Windows 7 64-bit


For Windows, you can use the File Change Notification API and have the OS call you when the item you're watching changes. Until that happens, your thread can sleep.

Having the OS call you is much preferred over setting up an infinite polling loop. If you run your polling loop without any sleeps, you'll peg the CPU at 100% utilization, force the fans to spool up to get rid of the extra heat, and deplete the laptop battery in 5 minutes. ;>


Id use lib event and forget the loop. (It is also platform independent). http://monkey.org/~provos/libevent/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜