C++ : Opening a file in non exclusive mode

后端 未结 5 643
深忆病人
深忆病人 2021-01-19 02:23

I have to develop an application which parses a log file and sends specific data to a server. It has to run on both Linux and Windows.

The problem appears when I wa

5条回答
  •  遥遥无期
    2021-01-19 02:38

    I'd make sure you don't keep files open. This leads to weird stuff if your app crashes for example. What I'd do:

    1. Abstract (reading / writing / rolling over to a new file) into one class, and arrange closing of the file when you want to roll over to a new one in that class. (this is the neatest way, and since you already have the roll-over code you're already halfway there.)
    2. If you must have multiple read/write access points, need all features of fstreams and don't want to write that complete a wrapper then the only cross platform solution I can think of is to always close the file when you don't need it, and have the roll-over code try to acquire exclusive access to the file a few times when it needs to roll-over before giving up.

提交回复
热议问题