Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,523 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,816 people online right now. Registration is fast and FREE... Join Now!




How to lock a file

 
Reply to this topicStart new topic

How to lock a file

no2pencil
11 Aug, 2008 - 06:24 PM
Post #1

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I need to recreate an error. Without making a file read-only, how on a Windows System can I open/access the file to have it in a 'Locked' or 'in use' state?

I do not have access to a compiler, or rexx, but I do have access to mks tool kit (Korn Shell).

Basically, what is happening is the Virus Scanner is scanning the file at the specific moment that a process tries to write to it, & all hell breaks loose, so I just need a quick way to recreate the error.

Thanks everyone!
User is online!Profile CardPM
+Quote Post

Tom9729
RE: How To Lock A File
11 Aug, 2008 - 06:53 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,459



Thanked: 10 times
Dream Kudos: 325
My Contributions
Would it be acceptable to just continuously write data to it with echo or something?
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How To Lock A File
11 Aug, 2008 - 06:57 PM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Tom9729 @ 11 Aug, 2008 - 10:53 PM) *

Would it be acceptable to just continuously write data to it with echo or something?

I need the file to be locked exclusively for writing by a process at the file system level.

Usually I just throw something together like this :

cpp

#include <stdio.h>

int main(void) {
int ch=0;
FILE * fp;

fp=fopen("counter.txt", "w+");
if(!fp) {
printf("Failed to lock the file, now exiting.\n");
return 1;
}
printf("File Locked...\n");

while(ch!=12) {
sleep(1);
}
fclose(fp);

return 0;
}


But they don't have a compiler on this machine...
User is online!Profile CardPM
+Quote Post

Tom9729
RE: How To Lock A File
11 Aug, 2008 - 07:01 PM
Post #4

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,459



Thanked: 10 times
Dream Kudos: 325
My Contributions
Could you download one?

TinyCC works just like GCC, and would easily fit on a flash drive or in a temp folder or something.

Edit: Actually I believe it would fit on a floppy if necessary.

This post has been edited by Tom9729: 11 Aug, 2008 - 07:02 PM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How To Lock A File
11 Aug, 2008 - 07:45 PM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I was able to achieve this with Rexx.

CODE

/* Open "mytext.txt" for reading, but only if it exists */
IF STREAM('counter.txt', 'C', 'OPEN BOTH') == 'READY:' THEN DO

   SAY 'counter.txt exists'

   /* Here you can use CHARIN and LINEIN to read from the file */

  DO A = 1 TO 100 BY 1
  sleep 1
  END

END

/* File can't be opened. SAY why */
ELSE SAY STREAM('counter.txt', 'D')
RETURN

User is online!Profile CardPM
+Quote Post

Tom9729
RE: How To Lock A File
12 Aug, 2008 - 06:03 AM
Post #6

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,459



Thanked: 10 times
Dream Kudos: 325
My Contributions
Cool, glad to hear you figured it out. icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 09:42PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month