Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 6th, 2006, 3:06 PM   #11
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Sorry to revive this thread, but I have a question that I think should be included in this thread.

The client utility (mentioned in first post) that the users will have writes to a single file. With 90+ users it is inevitable that multiple people could be trying to write (add service call) to the file at the same time. Before I deploy this program, I would like to add a function that will check the .DAT file to see if it is currently being edited before the actual write process begins.

I would greatly appreciate any info that can be provided on the creation of this function. Links, tutorials, etc.

Thanks,
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Jul 6th, 2006, 7:23 PM   #12
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 850
Rep Power: 4 The Dark is on a distinguished road
How are you opening the file at the moment in the utility?
If you are using ofstreams, you could add a sharing parameter to the open call or to the constructor call. _SH_DENYWR will stop others from opening the file for write at the same time. You would then just have to handle the case where the utility couldn't open the file, e.g. by waiting for a bit and then retrying.
Note: I haven't tried this, so it could be all wrong.
The Dark is offline   Reply With Quote
Old Jul 10th, 2006, 2:09 PM   #13
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Quote:
Originally Posted by The Dark
How are you opening the file at the moment in the utility?
If you are using ofstreams, you could add a sharing parameter to the open call or to the constructor call. _SH_DENYWR will stop others from opening the file for write at the same time. You would then just have to handle the case where the utility couldn't open the file, e.g. by waiting for a bit and then retrying.
Note: I haven't tried this, so it could be all wrong.
I'm just using fopen, fwrite, and fclose. Should I be using a different method?

Thanks,
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Jul 10th, 2006, 2:15 PM   #14
Zap
Guest
 
Posts: n/a
Some programs won't write to files properly if another program already has a file open. Forger told me that.
  Reply With Quote
Old Jul 10th, 2006, 2:53 PM   #15
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Quote:
Originally Posted by badbasser98
Before I deploy this program, I would like to add a function that will check the .DAT file to see if it is currently being edited before the actual write process begins.
Could you just write a function with fopen() to see if the .dat file is open before you allow a user to write to it, or do I misunderstand you?
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Jul 10th, 2006, 7:25 PM   #16
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 850
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by badbasser98
I'm just using fopen, fwrite, and fclose. Should I be using a different method?

Thanks,
-BB98
The fopen function doesn't have an option for file locking. What you can do, without having to rewrite your existing code, is to use the sopen() function with a third parameter of _SH_DENYWR, this will give you a file descriptor and then use fdopen() to convert this file descriptor to a FILE pointer.
The Dark is offline   Reply With Quote
Old Jul 11th, 2006, 8:23 AM   #17
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Quote:
Originally Posted by Prm753
Could you just write a function with fopen() to see if the .dat file is open before you allow a user to write to it, or do I misunderstand you?
The client does not keep the file open, it is opened for read only when the client is first started to verify the file exists and is within the size limit. The next time the file is opened is when they submit the entry, then it is opened for appending. I just want to prevent having multiple people trying to write to the file at the same time.

I think The Dark's solution will work, I just need to finish editing the code. The only question I have so far is, do I have to turn off the write lock? Or will that happen when the file is closed?

Thanks for the help.
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Jul 11th, 2006, 5:08 PM   #18
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 850
Rep Power: 4 The Dark is on a distinguished road
It will happen when the file is closed.
The Dark is offline   Reply With Quote
Old Jul 13th, 2006, 3:11 PM   #19
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
I got this to work as I wanted by initiating the lock immediatly after the program writes to the file for a short period of time. I noticed through trial and error that this was the vulnerable time when data loss happened during very close consecutive writes. I have tested this method quite extensively and have not experienced any data loss thus far with the lock in place.

Please let me know if this might present other problems I am not aware of.

Thanks for the help The Dark.

-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC