ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / External file functions
  • The lock was not set
  • Locking a file
  • Position in the file
  • Operating mode in Windows Vista (and later)
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Entirely or partially locks an external file. The file will be unlocked:
  • using the fUnlock function,
  • when the file is closed.
The application that locked the file can read this file and write into this file. The other applications cannot read this file or write into this file.
// Open an external file
FileID is int
ResLock is boolean
FileID = fOpen("C:\MyDirectories\File.txt", foReadWrite)
IF FileID <> -1 THEN
...
// Lock the external file
ResLock = fLock(FileID)
...
END
// Open an external file
MyTextFile is DiskFile
ResLock is boolean
IF fOpen(MyTextFile, "C:\MyDirectories\File.txt", foReadWrite) = True THEN
...
// Lock the external file
ResLock = fLock(MyTextFile)
...
END
Syntax
<Result> = fLock(<File used> [, <First byte to lock> [, <Number of bytes to lock>]])
<Result>: Boolean
  • True if the file was locked,
  • False otherwise.
<File used>: Integer or DiskFile variable
<First byte to lock>: Optional integer
Position of the first byte to lock (0 corresponds to the first byte of the file).
<Number of bytes to lock>: Optional constant or integer
  • Number of bytes to lock in the file.
  • Constant:
    fLockEmptyFilefLock has the same behavior whether the file is empty or not (see Remarks).
Remarks

The lock was not set

fLock returns False if the file was not locked. The lock can be set only if:
  • the external file is opened,
  • the file identifier is valid,
  • the file or the bytes to lock are not already locked by another computer or by another application.

Locking a file

  • The file is entirely locked if <First byte to lock> and <Number of bytes to lock> are not specified.
  • The file is locked from the first byte to lock (<First byte to lock>) until the end if <Number of bytes to lock> is not specified.
  • By default, fLock does not lock any bytes if the file is empty. In this case, several applications can successfully "lock" an empty file. However, the file cannot be overwritten as long as it is locked.
To get the same behavior no matter whether the file is empty or not, use the fLockEmptyFile constant.
Linux The lock is effective between two different sites or contexts. Unlike Windows, a process cannot lock a file that was previouly locked.

Position in the file

When opening a file, the current position corresponds to:
  • the first file byte (by default),
  • the last file byte if the file is opened in "addition" mode (foAdd constant).
This position can be modified by fSeek.
The current position does not change when fLock is used.
WINDEVUser code (UMC)

Operating mode in Windows Vista (and later)

If this function does not operate properly in Windows Vista (and later), check whether the file or directory used is not in one of the system directories (Windows directory or "Program Files" directory).
In Windows Vista (and later), with the UAC mechanism (User Account Control) enabled, you must have administrator privileges to handle and/or modify the files or directories in system directories (Windows directory or "Program Files" directory).
Programming tip: To handle and/or modify the files or directories without administrator privileges, you should:
  • avoid writing to the Windows directory or to the "Program Files" directory,
  • use the system directory of the application (returned by SysDir with the srAppDataCommun constant, for example).
Related Examples:
Handling text files Unit examples (WINDEV): Handling text files
[ + ] Handling "text" files with WINDEV:
- Create a text file
- Write into a text file
- Read in a text file
Handling text files Unit examples (WEBDEV): Handling text files
[ + ] This example explains how to handle "non HFSQL" files with WEBDEV and it allows you to:
- Create a text file
- Write into a text file
- Read in a text file
Business / UI classification: Business Logic
Component: wd290std.dll
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 11/17/2023

Send a report | Local help