vermarajeev (16th May 2007)
Hi Marcel,
The link only uses FILE* to lock and unlock the file. Is there a way by which I can use fstream or ifstream to lock a file. I read some basics to know what is locking about and have basic idea now. I tried out the solution but is not useful to me. My application uses ifstream to open and read the file. Since the application is developed using Qt, I'm looking for a portable solution.
Wysota-->
I too tried flock() and lockf(), and came to the conclusion that it works only on linux....Can you suggest some solution which runs both on windows and linux.
By the way dont QFile or QIODevice provide functionality to lock and unlock the files...Or isnt there any means I can use Qt to do that stuff.
Thanks
You cannot do that with Qt and there is no portable solution. If you want something portable, do it yourself, using platform dependent API.
I have recently done something similar at work, but unfortunately I also needed to lock files on samba shares, therefore the platform API functions were of no use ( on Mac and Win ) and I did a custom locking mechanism.
If you only need to lock local files, go ahead and use system API.
It is not necessary to have #ifdef's all over your app, just where you implement the functionality - for example the function(s) will be called the same and will take the same number of params both on Win and Linux.
Regards
You can get a FILE* from a C++ stream, you know... And then both flock and _lock_file will work.
While just implementing locking mechanism, I got one more idea.
Since the locking is to be restricted for the same application I can (I think)achieve this simply by associating an integer variable for each opened file, or just one array(or other appropiate data structure) for all opened files. for this I can make use of QStringList. If the same file is opened twice check in QStringList to see if file already present if so display some message to user "File already opened" if not open the file.
Will it not be feasible to implement the above said logic.
Thanks
What if one of the instances of the application crashes?
Crashes = gets killed
If you kill the application before releasing the pseudolock, you won't be able to open the same file again. You have to have some safetly mechanism to prevent that.
Ok, so it can be dangerous. What do you mean when you talk pseudolock???
Can I provide some safety mechanism using the above specified solution by me???You have to have some safetly mechanism to prevent that
The solution I'm looking should be portable and as per my searching I didnt get any. I came across semaphores too but found that it works for linux.
As Marcel says, I need to use some system dependent API's... What API's????
I'll try out the solution and let you know the status....
Thanks
Well, the mechanism you describe doesn't do any locking, so I called it a pseudolock.
If you can get some process identifier on Windows then yes. Store the PID along the filename which the particular process handles in your lock data and then when you want to open the same file check if a process with this identifier exists. If not, it means that the entry is stale and can be removed.Can I provide some safety mechanism using the above specified solution by me???
You can use different mechanisms on different platforms.The solution I'm looking should be portable and as per my searching I didnt get any. I came across semaphores too but found that it works for linux.
http://en.wikipedia.org/wiki/APIWhat API's????
How to convert C++ stream to FILE*......You can get a FILE* from a C++ stream, you know... And then both flock and _lock_file will work.![]()
You'll have to search through available documentation of ostream probably. There should be a way to access the buffer which you can then associate with a file descriptor or file handler or whatever.
Bookmarks