PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US

  • Managing the 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 format
  • Open an infrared port
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Opens an external file (ANSI or UNICODE) in order to handle it by programming. You also have the ability to use fLoadText.
Notes:
Versions 15 and later
Android This function is now available for the Android applications.
New in version 15
Android This function is now available for the Android applications.
Android This function is now available for the Android applications.
Versions 16 and later
Universal Windows 10 App This function is now available for the Windows Phone applications.
New in version 16
Universal Windows 10 App This function is now available for the Windows Phone applications.
Universal Windows 10 App This function is now available for the Windows Phone applications.
Versions 17 and later
iPhone/iPad This function is now available for the iPhone/iPad applications.
New in version 17
iPhone/iPad This function is now available for the iPhone/iPad applications.
iPhone/iPad This function is now available for the iPhone/iPad applications.
Versions 18 and later
Android Widget This function is now available in Android Widget mode.
Universal Windows 10 App This function is now available in Windows Store apps mode.
New in version 18
Android Widget This function is now available in Android Widget mode.
Universal Windows 10 App This function is now available in Windows Store apps mode.
Android Widget This function is now available in Android Widget mode.
Universal Windows 10 App This function is now available in Windows Store apps mode.
Versions 21 and later
Universal Windows 10 App This function is now available in Universal Windows 10 App mode.
New in version 21
Universal Windows 10 App This function is now available in Universal Windows 10 App mode.
Universal Windows 10 App This function is now available in Universal Windows 10 App mode.
WINDEVWEBDEV - Server codeReports and QueriesJavaPHPAjaxUser code (UMC)
// Open an external file
ResOpening = fOpen("C:\MyDirectories\File.txt", foReadWrite)
IF ResOpening <> -1 THEN
...
END
Windows Mobile
// Open an external file
ResOpening = fOpen("\MyDirectories\File.txt", foReadWrite)
IF ResOpening <> -1 THEN
...
END
Windows Mobile
// Open an infrared port
ResOpening = fOpen("COM3", foWrite)
IF ResOpening <> -1 THEN
fWrite(ResOpening, "Bytes to transmit")
END
Syntax
<Result> = fOpen(<File Path> [, <Opening mode>])
<Result>: Integer
Corresponds to:
  • 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 find out the error details, use ErrorInfo associated with the errMessage constant.
<File Path>: Character string (with quotes)
Name and full (or relative) path of file (up to 260 characters). A UNC path can be used.
Versions 15 and later
AndroidAndroid Widget This parameter can correspond to a full path or to a relative path in relation 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 has the rights to write into its setup directory or into one of its sub-directories, as well as onto the external memory (SDCard).
New in version 15
AndroidAndroid Widget This parameter can correspond to a full path or to a relative path in relation 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 has the rights to write into its setup directory or into one of its sub-directories, as well as onto the external memory (SDCard).
AndroidAndroid Widget This parameter can correspond to a full path or to a relative path in relation 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 has the rights to write into its setup directory or into one of its sub-directories, as well as onto the external memory (SDCard).
Windows Mobile This path must be a full path or a UNC path. It cannot be a relative path.
Versions 16 and later
Universal Windows 10 App Windows Phone: The file path corresponds to a relative path in relation to the current directory. The current directory corresponds to the data directory of application.
Reminder: Only the files found in the data directory associated with the application can be handled.
New in version 16
Universal Windows 10 App Windows Phone: The file path corresponds to a relative path in relation to the current directory. The current directory corresponds to the data directory of application.
Reminder: Only the files found in the data directory associated with the application can be handled.
Universal Windows 10 App Windows Phone: The file path corresponds to a relative path in relation to the current directory. The current directory corresponds to the data directory of application.
Reminder: Only the files found in the data directory associated with the application can be handled.
Versions 17 and later
iPhone/iPad This parameter can correspond to a full path or to a relative path in relation to the current directory (returned by fCurrentDir). This parameter is case sensitive.
Reminder: On iPhone/iPad, an application has the rights to write into its setup directory or into one of its sub-directories.
New in version 17
iPhone/iPad This parameter can correspond to a full path or to a relative path in relation to the current directory (returned by fCurrentDir). This parameter is case sensitive.
Reminder: On iPhone/iPad, an application has the rights to write into its setup directory or into one of its sub-directories.
iPhone/iPad This parameter can correspond to a full path or to a relative path in relation to the current directory (returned by fCurrentDir). This parameter is case sensitive.
Reminder: On iPhone/iPad, an application has the rights to write into its setup directory or into one of its sub-directories.
<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.
    foCreateCreating 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.
    • If the file does not exist, open the file in "addition" mode.
    • If the file exists, 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:
    Versions 17 and later
    foAnsi
    New in version 17
    foAnsi
    foAnsi
    Ansi file. This constant is used to manage an Ansi file when the "Use the Unicode strings at run time" 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 the Ansi strings at run time" mode from version 17.
    foUnicodeUnicode file. This constant is used to manage a Unicode file when the "Use the Ansi strings at run time" 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 the Unicode strings at run time" mode from version 17.
    AndroidAndroid Widget JavaPHP This constant is not available.
  • File locking mode:
    foWriteLockThe other applications cannot modify the current file.
    LinuxAndroidAndroid Widget Windows MobileJavaPHP This constant is not available.
    foReadLockThe other applications cannot read the current file.
    LinuxAndroidAndroid Widget Windows MobileJavaPHP This constant is not available.
    foWriteOpen the file in "write-only" mode. This file can only be modified.
    AndroidAndroid Widget Java This constant is not available.
    foReadOpen the file in "read-only" mode. This file can only be read.
    foReadWrite
    (Default value)
    Open the file in "read/write" mode. This file can be read and modified (equivalent to foRead+foWrite).
    Versions 16 and later
    foAutomaticDeletion
    New in version 16
    foAutomaticDeletion
    foAutomaticDeletion
    The file is locked when it is opened and it will be automatically deleted when it is closed (call to fClose or at the end of the application).
    If this file is opened by several applications, the file will be automatically deleted when the last application closes the file (call to fClose or at the end of the application).
    LinuxWindows Mobile This constant is not available.
    Java If the file is used by another application when it is closed (call to fClose or at the end of the application), it will not be deleted.
  • Versions 19 and later
    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.
    New in version 19
    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.
    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

Managing the errors

fOpen generates 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.
Note: The opening in modification of a file found in a write-protected diskette is successful.
WINDEVWEBDEV - Server codeReports and QueriesUser 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 used is not found in one of the system directories (Windows directory or "Program Files" directory).
Indeed, in Windows Vista (and later), with the UAC mechanism (User Account Control) enabled, you must have the administrator privileges to handle and/or modify the files found in the system directories (Windows directory or "Program Files" directory).
Programming tip: To handle and/or modify the files without having administrator privileges, you should:
  • avoid writing into the Windows directory or into the "Program Files" directory,
  • use the system directory corresponding to the application (returned by SysDir associated with the srCommonAppData constant).

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 proposed 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 byte of the file,
  • 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 by the value 0. In this case, fOpen tries to open the file in read/write.
WINDEVWEBDEV - Server codeReports and QueriesUniversal Windows 10 AppAjaxUser code (UMC)

Locking and unlocking

A file locked when it is opened (foReadLock or foWriteLock constant) will be automatically unlocked when it is closed (by fClose).
WINDEVWEBDEV - Server codeReports and QueriesWindows MobileAjaxUser code (UMC)

Managing text files in UNICODE format

fOpen is used to read and write text files in UNICODE "UTF-16 little endian" format.
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.
Windows Mobile

Open an infrared port

fOpen can also be used to open an infrared port. You will have the ability, for example, to send a sequence of bytes to the port.
Once it is opened, the infrared port behaves like an external file. It can be handled by the functions for external files.
To open an infrared port, use the following syntax:
<Result> = fOpen("COM3:", foWrite)
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
Components
WINDEVWEBDEV - Server codeReports and Queries wd230std.dll
Windows Mobile wp230std.dll
Java wd230java.jar
Linux wd230std.so
Android wd230android.jar
Minimum required version
  • 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
Feb. 21 2015