Sunday 15 June 2014

windows - How to write to file in C++ without locking it? -



windows - How to write to file in C++ without locking it? -

c++ in windows 7. when writing log file, set breakpoint, or programme gets stuck @ something. when seek peek in logfile programme says "the file cannot opened because in utilize process". thats true, i've worked other programs still allows reading logfile while writing it, know should possible. tried _fsopen , unlocking file without success.

file* logfile; //fopen_s(&logfile, "log.log", "w"); logfile = _fsopen("log.log", "w", _sh_denyno); if (!logfile) throw "fopen"; _unlock_file(logfile);

if have log-file open total sharing-mode, others still stopped opening exclusive access, or deny-write.

seems sec programme wants more access compatible.

also, guess want append log, use mode "a" instead of "w".

last, do not phone call _unlock_file unless called _lock_file on same file previously.

there way want though:

open file without access, , utilize opportunistic locks.

raymond chen's blog the old new thing has nice example: http://blogs.msdn.com/b/oldnewthing/archive/2013/04/15/10410965.aspx

c++ windows file file-io locking

No comments:

Post a Comment