2

i have c++ program (turbo c++ ver 3) that write/read data into files. when multiple instances of the program is run and at particular point of execution , when the two instance of the program open and write into the file at the same time , some information is lost

if(!filein)

is not ideal for this situation. Its a issue with sync i guess

Is there any way that i can check if the file is opened or not before performing any operation

thank you

  • Please post more code. Like the code used to open the files. – sqreept Dec 28 '12 at 18:20
  • 2
    You need file locks, which are not implemented in the standard library. See this question: http://stackoverflow.com/q/839856/1175253 – Sam Dec 28 '12 at 18:20
  • You need a mutex to coordinate access to the file. Can you use operating system APIs? If so, what platform are you targeting? – Carey Gregory Dec 28 '12 at 18:24
  • @CareyGregory Its not a complex program. im an absolute beginner and i have no idea what u r talking about – user1925363 Dec 28 '12 at 18:27
  • @Sam im using turbo c++. i cant find any LockFileEx() but there is lock(); can i use it – user1925363 Dec 28 '12 at 18:29
  • Please add more info about the compiler and target API/headers. In newer versions, you should have CreateFile() and even LockFile() (win32). But then, you can't/shouldn't use fstreams. Sometimes, you need to 'elevate' the Windows API version by setting the macro 'WINVER' to an appropriate value, e.g. 0x0501 for Windows XP. – Sam Dec 28 '12 at 18:52
  • If boost is not an option, you can indeed use lock(), open(), sopen(), read(), write() and close(), they seem to be defined in "IO.H". – Sam Dec 28 '12 at 21:06
  • @Sam - It doesn't matter if it's a simple program. You need some way of controlling access to the file. What operating system are you using? Windows? – Carey Gregory Dec 28 '12 at 22:34
  • @Carey Gregory - You're right, but there are several methods to do this, so yes, we need to know the target platform (most likely Windows, since turbo C++ is for Windows). I claim, that file locks are mandatory in such implementations, whether access synchronization is done using a global mutex for all processes, or another waiting mechanism on the file itself like this: http://stackoverflow.com/q/7621248/1175253 (I'd prefer this approach). – Sam Dec 28 '12 at 22:42
  • @Sam - Sure, you should use file locking, but using that to synchronize access means you have to use spin locks, which are really inefficient. I would use a mutex to synchronize access and locking to prevent processes other than my own from accessing the file. (And, honestly, I didn't even know Turbo C++ still existed.) – Carey Gregory Dec 28 '12 at 22:47
  • The OP may consider replacing his toolchain with MSVC, or MinGW + Code::Blocks or Eclipse. I'd also choose mutexes, however, that wouldn't prevent another program from trying to access that file. In my previous comment, I failed to explain my thoughts thoroughly: Instead of spinning after a failed open(), I'd just wait on that global mutex, if the file is locked by another instance of the same program. – Sam Dec 28 '12 at 23:02

1 Answers1

1

If you have boost available then the easiest/best way is to embed the mutex into the file itself using boost::interprocess::file_lock (docs)

Alastair Maw
  • 5,373
  • 1
  • 38
  • 50