AYUDA EN LÍNEA
 WINDEVWEBDEV Y WINDEV MOBILE

Este contenido se ha traducido automáticamente.  Haga clic aquí  para ver la versión en inglés.
Ayuda / WLanguage / Funciones WLanguage / Funciones estándar / Funciones de archivos externos
  • 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
WindowsLinuxJavaReportes y ConsultasCódigo de Usuario (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Código Navegador
WINDEV Mobile
AndroidWidget Android iPhone/iPadIOS WidgetApple WatchMac Catalyst
Otros
Procedimientos almacenados
Open an external file (ANSI or UNICODE) to manipulate it programmatically.
Remarks:
WINDEVWEBDEV - Código ServidorReportes y ConsultasJavaCódigo de Usuario (UMC)PHPAjax
// Open an external file
ResOpening is int
ResOpening = fOpen("C:\MyDirectories\File.txt", foReadWrite)
IF ResOpening <> -1 THEN 
	...
END
WINDEVWEBDEV - Código ServidorReportes y ConsultasJavaCódigo de Usuario (UMC)PHPAjax
// Open an external file
MyExternalFile is DiskFile
MyExternalFile = fOpen("C:\MyDirectories\File.txt", foReadWrite)
IF NOT ErrorOccurred THEN
	...
END
WINDEVWEBDEV - Código ServidorReportes y ConsultasJavaCódigo de Usuario (UMC)PHPAjax
// Open an external file
MyExternalFile is DiskFile
ResOuverture is TO boolean
ResOpening = fOpen(MyExternalFile, "C:\MyDirectories\File.txt", foReadWrite)
IF ResOpening THEN
	...
END
Sintaxis

Open an external file Ocultar los detalles

<Result> = fOpen(<File to manipulate> [, <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 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.
    PHP Not available.
<File to manipulate>: Character string
Name and full or relative path of the file (up to 260 characters). A UNC path can be used.

AndroidWidget Android 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 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 "Utilizar cadenas ANSI en ejecución" 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 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).
    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 fClose 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.
    LinuxAndroidWidget Android 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.
    AndroidWidget Android Java This constant is not available.
    foWriteLockThe other applications cannot modify the current file.
    LinuxAndroidWidget Android 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.
    AndroidWidget Android JavaPHP This constant is not available.

Opening an external file and initializing a DiskFile variable Ocultar los detalles

<Result> = fOpen(<DiskFile> , <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.

AndroidWidget Android 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 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 "Utilizar cadenas ANSI en ejecución" 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 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).
    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 fClose 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.
    LinuxAndroidWidget Android 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.
    AndroidWidget Android Java This constant is not available.
    foWriteLockThe other applications cannot modify the current file.
    LinuxAndroidWidget Android 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.
    AndroidWidget Android JavaPHP This constant is not available.
Observaciones

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.
WINDEVWEBDEV - Código ServidorReportes y ConsultasCódigo de Usuario (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 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 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.
WINDEVWEBDEV - Código ServidorReportes y ConsultasCódigo de Usuario (UMC)Ajax

Locking and unlocking

If a file is locked when opened (foReadLock or foWriteLock), it will be automatically unlocked when closed (with fClose).
WINDEVWEBDEV - Código ServidorReportes y ConsultasAndroidWidget Android JavaCódigo de Usuario (UMC)Ajax

Managing UNICODE text files

fOpen is used to read and write UNICODE (UTF-16 Little Endian) text files.
  • fOpen(<File name>, foRead + foUnicode): Opens a Unicode file. If the "Current Unicode" mark (FFFE) is found at the beginning of the file, it is automatically read.
  • fOpen(<File name>, foCreate + foUnicode): Creates a Unicode file. The "Current Unicode" mark (FFFE) is automatically added at the beginning of the file.
  • fOpen (<File name>, 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.
Clasificación Lógica de negocio / UI: Lógica de negocio
Componente: wd300std.dll
Versión mínima requerida
  • Versión 9
Esta página también está disponible para…
Comentarios
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 02 2015

Última modificación: 27/03/2025

Señalar un error o enviar una sugerencia | Ayuda local