ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / External file functions
  • Handling errors
  • Operating mode in Windows Vista (and later)
  • File opening mode
  • Multiple openings of the same file
  • Position in the file
  • Concatenating several external files
  • Compatibility between different versions of WINDEV/WEBDEV
  • Locking and unlocking
  • Managing UNICODE text files
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
Open an external file (ANSI or UNICODE) to manipulate it programmatically.
Remarks:
Example
// Ouverture d'un fichier externe
MonFichierExterne is DiskFile
ResOuverture is boolean
ResOuverture = MonFichierExterne.Open("C:\MesRépertoires\Fichier.txt", foReadWrite)
IF ResOuverture THEN
	...
END
Syntax

Opening an external file and initializing a DiskFile variable Hide the details

<Result> = <DiskFile>.fOpen(<File to manipulate> [, <Opening mode>])
<Result>: Boolean
  • True if the file was opened and associated with variable of type DiskFile,
  • False otherwise. To get more details on the error, use ErrorInfo with the errMessage constant.
<DiskFile>: DiskFile variable
Name of the DiskFile variable to be associated with the manipulated text file.
<File to manipulate>: Character string
Name and full or relative path of the file (up to 260 characters). A UNC path can be used.

AndroidAndroid Widget This parameter can correspond to a full path or a path relative to the current directory (returned by fCurrentDir). This parameter is case-sensitive.
Reminder: In Android, the file system is read-only on the device and on the emulator. An application can only write to its installation directory or one of its subdirectories, as well as to the external memory (SDCard).


iPhone/iPadIOS WidgetMac Catalyst This parameter can correspond to a full path or a path relative to the current directory (returned by fCurrentDir). This parameter is case-sensitive.
Reminder: On iPhone or iPad, an application has the rights to write to its installation directory or one of its subdirectories
<Opening mode>: Optional constant (or combination of constants)
Constants used to define the opening mode of the file, the access mode to the file and the lock mode of the file.
  • File opening mode:
    foAddOpens the file in "addition" mode. At opening, the current position corresponds to the last byte of the file.
    foCreateCreates the file. If the file already exists (same name, same directory), it is deleted and created again.
    foCreateIfNotFoundCreates the file only if it does not exist. The current position corresponds to the first byte of the file.
  • File access mode. This type is used by <DiskFile variable>.ReadLine and <DiskFile variable>.WriteLine to define the type of information to read and write in the external file:
    foAnsiAnsi file. This constant is used to manage an Ansi file when the "Use Unicode strings at runtime" mode is enabled in the project configuration.
    Mode used by default:
    • in the WINDEV and WEBDEV projects earlier than version 17.
    • in the configurations of WINDEV and WEBDEV projects that use the "Use Ansi strings at runtime" mode from version 17.
    foUnicodeUnicode file. This constant is used to manage a Unicode file when the "Use ANSI strings at runtime" mode is enabled in the project configuration.
    Mode used by default:
    • in the WINDEV Mobile projects regardless of the mode and version used.
    • in the configurations of WINDEV and WEBDEV projects that use the "Use Unicode strings at runtime" mode from version 17.
    PHP This constant is not available.
  • Mode for locking the file:
    foAutomaticDeletionThe file is locked when opened, and will be automatically deleted when closed (when <DiskFile variable>.Close is called, or when the application is closed).
    If multiple applications are using the file, it will be automatically deleted when the last application closes it (when <DiskFile variable>.Close is called, or at the end of the application).
    Linux This constant is not available.
    Java The file will not be deleted if it is being used by another application when it is closed (when <DiskFile variable>.Close is called, or when the application is closed).
    foReadOpens the file in "read-only" mode. This file can only be read.
    foReadLockThe other applications cannot read the current file.
    LinuxAndroidAndroid Widget JavaPHP This constant is not available.
    foReadWrite
    (Default value)
    Opens the file in "read/write" mode. This file can be read and modified (equivalent to foRead+foWrite).
    foWriteOpens the file in "write-only" mode. This file can only be modified.
    AndroidAndroid Widget Java This constant is not available.
    foWriteLockThe other applications cannot modify the current file.
    LinuxAndroidAndroid Widget JavaPHP This constant is not available.
  • Other options:
    foSequentialAccessOptimizes the management of caches for the file by specifying to Windows that the file will be read from the beginning to the end.
    AndroidAndroid Widget JavaPHP This constant is not available.
Remarks

Handling errors

<DiskFile variable>.Open throws an error in the following cases:
  • the file does not exist,
  • the file is locked by another computer or by another application,
  • the user has no read or write rights on the file to open.
WINDEVWEBDEV - Server codeReports and QueriesUser code (UMC)

Operating mode in Windows Vista (and later)

If this function does not work 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: If you need to manipulate / modify files or directories, without needing administrator privileges, it is advisable:
  • 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).

File opening mode

By default, if the access mode to the file is not specified, the file is opened according to the file attribute (defined in Windows).
An error occurs if the specified access mode does not correspond to the file attribute.
A file in "read/write" mode can be opened in all the access modes supported by <DiskFile variable>.Open (foWrite, foRead and foReadWrite constants).
A file in "read-only" mode can only be opened in "read-only" mode (foRead constant).

Multiple openings of the same file

The same file can be opened several times simultaneously. Each opening is associated with a different file identifier. Each version of the file must be handled individually via its own identifier.

Position in the file

When opening a file, the current position corresponds to:
  • the first file byte,
  • the last byte of the file is the file is opened in "addition" mode (foAdd constant).
This position can be modified by <DiskFile variable>.Seek.

Concatenating several external files

To concatenate several external files, you must:
  • open the destination file in "addition" mode (foAdd constant),
  • open the source file in "read-only" mode (foRead or foReadWrite constant),
  • read the source file line by line with <DiskFile variable>.ReadLine,
  • write into the destination file line by line with <DiskFile variable>.WriteLine.

Compatibility between different versions of WINDEV/WEBDEV

The foText constant (opening in text mode) and the foBinary constant (opening in binary mode) are not required anymore. They are replaced with the value 0. In this case, <DiskFile variable>.Open tries to open the file in read/write mode.
WINDEVWEBDEV - Server codeReports and QueriesUser code (UMC)Ajax

Locking and unlocking

If a file is locked when opened (foReadLock or foWriteLock), it will be automatically unlocked when closed (with <DiskFile variable>.Close).
WINDEVWEBDEV - Server codeReports and QueriesAndroidAndroid Widget JavaUser code (UMC)Ajax

Managing UNICODE text files

<DiskFile variable>.Open is used to read and write UNICODE (UTF-16 Little Endian) text files.
  • <DiskFile>.Open(<File path>, foRead + foUnicode): Opens a Unicode file. If the "Current Unicode" mark (FFFE) is found at the beginning of the file, it is automatically read.
  • <DiskFile>.Open(<File path>, foCreate + foUnicode): Creates a Unicode file. The "Current Unicode" mark (FFFE) is automatically added at the beginning of the file.
  • <DiskFile>.Open(<File path>, foCreateIfNotFound + foUnicode): Creates a Unicode file, if it doesn't exist. The "Current Unicode" mark (FFFE) is automatically added at the beginning of the file if it has been created. If the file exists and starts with the "Current Unicode" mark (FFFE), this mark is automatically read.
Business / UI classification: Business Logic
Component: wd300std.dll
Minimum version required
  • Version 28
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 09/26/2024

Send a report | Local help