ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / External file functions
  • Handling errors
  • Operating mode in Windows Vista (and later)
  • Opening mode of file
  • Multiple openings of the same file
  • Position in the file
  • Concatenating several external files
  • Compatibility between the different versions of WINDEV/WEBDEV
  • Locking and unlocking
  • Managing text files in UNICODE
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
Opens an external file (ANSI or UNICODE) in order to handle it through programming. You can also use fLoadText.
Remarks:
Reports and Queries
// Open an external file
ResOpening = fOpen("C:\MyDirectories\File.txt", foReadWrite)
IF ResOpening <> -1 THEN
...
END
Reports and Queries
// Open an external file
MyFile is DiskFile
ResOpening = fOpen("C:\MyDirectories\File.txt", foReadWrite)
IF NOT ErrorOccurred THEN
...
END
Syntax
<Result> = fOpen(<File path> [, <Opening mode>])
<Result>: Integer or DiskFile variable
Corresponds to:
  • an integer:
    • the identifier of the external file. This identifier will be used by all the functions for handling the external files.
    • -1 if an error occurred. To get more details on the error, use ErrorInfo with the errMessage constant.
  • a variable of type DiskFile. If an error occurs, the ErrorOccurred variable is set to True. To get more details on the error, use ErrorInfo with the errMessage constant.
<File path>: Character string
Name and full (or relative) path of file (up to 260 characters). A UNC path can be used.
<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.
  • Opening mode of file:
    foAddOpen the file in "addition" mode. At opening, the current position corresponds to the last byte of the file.
    foCreateCreate the file. If the file already exists (same name, same directory), this file is deleted and recreated.
    foCreateIfNotExistCreate the file only if this file does not exist. The current position corresponds to the first byte of the file.
  • Access mode to the file. This type is used by fReadLine and fWriteLine 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.
  • File locking mode:
    foAutomaticDeletionThe file is locked when opened, and will be automatically deleted when closed (when fClose 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 fClose is called, or at the end of the application).

    foReadOpen the file in "read-only" mode. This file can only be read.
    foReadLockThe other applications cannot read the current file.
    foReadWrite
    (Default value)
    Open the file in "read/write" mode. This file can be read and modified (equivalent to foRead+foWrite).
    foWriteOpen the file in "write-only" mode. This file can only be modified.
    foWriteLockThe other applications cannot modify the current file.
  • 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.
Remarks

Handling errors

fOpen 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.
  • a Unicode string was used for <File path> in an operating system such as Windows 98 or Windows Me.
Remark: The opening in modification of a file found in a write-protected diskette is successful.
Reports and Queries

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).

Opening mode of file

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 fOpen (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 fSeek.

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 fReadLine,
  • write into the destination file line by line with fWriteLine.

Compatibility between the 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, fOpen tries to open the file in read/write mode.
Reports and Queries

Locking and unlocking

If a file is locked when opened (foReadLock or foWriteLock), it will be automatically unlocked when closed (with fClose).
Reports and Queries

Managing text files in UNICODE

fOpen is used to read and write text files in "UTF-16 Little Endian" UNICODE characters.
The syntax:
  • fOpen(<File name>, foRead + foUnicode): is used to open a Unicode file. If the "Current Unicode" mark (FFFE) is found at the beginning of the file, it is automatically read.
  • fOpen(<File name>, foCreation + foUnicode): is used to create a Unicode file. The "Current Unicode" mark (FFFE) is automatically added at the beginning of the file.
  • fOpen (<File name>, foCreationIfNotFound + foUnicode): is used to create a UNICODE file if this file does not exist. The "Current Unicode" mark (FFFE) is automatically added at the beginning of the file this one is actually created. If the file exists and if it starts with the "Current Unicode" mark (FFFE), this mark is automatically read.
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
Handling text files Unit examples (WINDEV Mobile): Handling text files
[ + ] Handling external "text" files:
- 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
Example ReadFile
PROCEDURE ReadFile(ArquivoTxt is string, TipoUnicodeToAnsi is string)

//Le arquivo texto
INFO(TIMESYS())

// Declare the variables
PathFile is string = ArquivoTxt
IdFile, ResCloseFile, FileID, NumeroLinha is int
Line is string

NextTitle("Atencao")

IF fFileExist(ArquivoTxt) = true

// Open the file
FileID = fOpen(PathFile,foReadWrite)
IF IdFile = -1 THEN
Error(ErrorInfo(errMessage))
ELSE

if TipoAnsiUnicode = "UnicodeToAnsi"
//Converte de Unicode para Ansi
Line = UnicodeToAnsi(fReadLine(FileID))
else
Line = fReadLine(FileID)
end

//Linha 1
NumeroLinha = 1
/// ----COLOQUE AQUI O PROGRESS BAR = 1
//Info("Linha:" + NumeroLinha, Line) ///----------------- LINHA LIDA
//Inclua aqui a SUB Procedure de importacao dos dados

WHILE Line <> EOT AND Line <> ""

//Proxima linha
NumeroLinha += 1
/// ----COLOQUE AQUI O PROGRESS BAR + 1
Line = fReadLine(FileID)
//Info("Linha:" + NumeroLinha, Line) ///----------------- LINHA LIDA
//Inclua aqui a SUB Procedure de importacao dos dados
END
IF Line = "" THEN Error(ErrorInfo(errMessage))
IF Line = EOT THEN Info("Final do Arquivo.")
ResCloseFile = fClose(IdFile)
IF ResCloseFile = -1 THEN
//Error(ErrorInfo(errMessage))
END

END

ELSE
Info("Arquivo não encontrado!")
END

info(NumeroLinha,TIMESYS())
adrianoboller
21 Feb. 2015

Last update: 07/04/2023

Send a report | Local help