This chapter provides reference material for the file streaming API.
The header file FileStream.h
declares the API that this chapter describes. For more information on file streaming, see the chapter "Files and Databases" in the Palm OS Programmer's Companion, vol. I.
File Streaming Constants
Primary Open Mode Constants
This section lists constants passed in the openMode
parameter to the FileOpen()
function. These constants specify the mode in which a file stream is opened.
For each file stream, you must pass to the FileOpen()
function only one of the primary mode selectors listed.
-
fileModeReadOnly
- Open for read-only access
-
fileModeReadWrite
- Open/create for read/write access, discarding any previous version of stream
-
fileModeUpdate
- Open/create for read/write, preserving previous version of stream if it exists
-
fileModeAppend
- Open/create for read/write, always writing to the end of the stream
Secondary Open Mode Constants
You can use the |
operator (bitwise inclusive OR) to append to a primary mode selector one or more of the secondary mode selectors listed below.
-
fileModeDontOverwrite
- Prevents
fileModeReadWrite
from discarding an existing stream having the same name; may only be specified together withfileModeReadWrite
-
fileModeLeaveOpen
- Leave stream open when application quits. Most applications should not use this option.
-
fileModeExclusive
- No other application can open the stream until the application that opened it in this mode closes it.
-
fileModeAnyTypeCreator
- Accept any type/creator when opening or replacing an existing stream. Normally, the
FileOpen()
function opens only streams having the specified creator and type. Setting this option enables theFileOpen
function to open streams having a type or creator other than those specified. -
fileModeTemporary
- Delete the stream automatically when it is closed. For more information, see Comment section of
FileOpen()
function description.
File Streaming Functions
FileClearerr Function
Purpose
Clear I/O error status, end of file error status, and last error.
Declared In
FileStream.h
Prototype
Err FileClearerr ( FileHand stream )
Parameters
Returns
0 if no error, or a fileErr
code if an error occurs. See the section "File Streaming Error Codes" for more information.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileGetLastError()
, FileRewind()
FileClose Function
Purpose
Close the file stream and destroy its handle. If the stream was opened with fileModeTemporary
, it is deleted upon closing.
Declared In
FileStream.h
Prototype
Err FileClose ( FileHand stream )
Parameters
Returns
0 if no error, or a fileErr
code if an error occurs. See the section "File Streaming Error Codes" for more information.
Compatibility
Implemented only if 3.0 New Feature Set is present.
FileControl Function
Purpose
Perform the operation specified by the op
parameter on the stream
file stream.
Declared In
FileStream.h
Prototype
Err FileControl ( FileOpEnum op, FileHand stream, void *valueP, Int32 *valueLenP )
Parameters
-
op
- The operation to perform, and its associated formal parameters. See the Comments section for a list of possible values.
-
→
stream
- Open stream handle if required for file stream operation.
-
↔
valueP
- Pointer to value or buffer, as required. This parameter is defined by the selector passed as the value of the
op
parameter. For details, see the Comments section. -
↔
valueLenP
- Pointer to value or buffer, as required. This parameter is defined by the selector passed as the value of the
op
parameter. For details, see the Comments section.
Returns
Returns either a value defined by the selector passed as the argument to the op
parameter, or an error code resulting from the requested operation. For details, see the Comments section.
Comments
Normally, you do not call the FileControl()
function yourself; it is called for you by most of the other file streaming functions and macros to perform common file streaming operations. You can call FileControl()
yourself to enable specialized read modes.
-
fileOpNone
- No-op.
-
fileOpDestructiveReadMode
- Enter destructive read mode, and rewind stream to its beginning. Once in this mode, there is no turning back: stream's contents after closing (or crash) are undefined.
- Destructive read mode deletes blocks as data are read, thus freeing storage automatically. Once in destructive read mode, you cannot re-use the file stream—the contents of the stream are undefined after it is closed or after a crash.
- Writing to files opened without write access or those that are in destructive read state is not allowed; thus, you cannot call the
FileWrite()
,FileSeek()
, orFileTruncate()
functions on a stream that is in destructive read mode. One exception to this rule applies to streams that were opened in "write + append" mode and then switched into destructive read state. In this case, theFileWrite()
function can append data to the stream, but it also preserves the current stream position so that subsequent reads pick up where they left off (you can think of this as a pseudo-pipe). - ARGUMENTS:
-
stream
= open stream handle -
valueP
=NULL
-
valueLenP
=NULL
- RETURNS:
- zero on success;
-
fileErr
... on error -
fileOpGetEOFStatus
- Get end-of-file status (like C runtime's
feof
) (err =fileErrEOF
). Indicates end of file condition. UseFileClearerr()
to clear this error status. - ARGUMENTS:
-
stream
= open stream handle -
valueP
=NULL
-
valueLenP
=NULL
- RETURNS:
- zero if not end of file;
- non-zero if end of file
-
fileOpGetLastError
- Get error code from last operation on stream, and clear the last error code value. Doesn't change status of EOF or I/O errors —use
FileClearerr()
to reset all error codes. - ARGUMENTS:
-
stream
= open stream handle -
valueP
=NULL
-
valueLenP
=NULL
- RETURNS:
- Error code from last file stream operation
-
fileOpClearError
- Clear I/O and EOF error status and last error.
- ARGUMENTS:
-
stream
= open stream handle -
valueP
=NULL
-
valueLenP
=NULL
- RETURNS:
- zero on success;
fileErr
... on error -
fileOpGetIOErrorStatus
- Get I/O error status (like C runtime's
ferror
). UseFileClearerr()
to clear this error status. - ARGUMENTS:
-
stream
= open stream handle -
valueP
=NULL
-
valueLenP
=NULL
- RETURNS:
- zero if not I/O error;
- non-zero if I/O error is pending.
-
fileOpGetCreatedStatus
- Find out whether file was created by
FileOpen()
function - ARGUMENTS:
-
stream
= open stream handle -
valueP
= Pointer to Boolean -
valueLenP
= Pointer toInt32
variable set to sizeof(Boolean) - RETURNS:
- zero on success;
fileErr
... on error. The Boolean variable will be set to non-zero if the file was created. -
fileOpGetOpenDbRef
- Get the open database reference (handle) of the underlying database that implements the stream (
NULL
if none); this is needed for performing Palm OS-specific operations on the underlying database, such as changing or getting creator and type, version, backup/reset bits, and so on. - ARGUMENTS:
-
stream
= open stream handle -
valueP
= Pointer toDmOpenRef
variable -
valueLenP
= Pointer to Int32 variable set to sizeof(DmOpenRef) - RETURNS:
- zero on success;
fileErr
... on error. TheDmOpenRef
variable will be set to the file's open db reference that may be passed to Data Manager calls; - WARNING: Do not make any changes to the data of the underlying database -- doing so will corrupt the file stream.
-
fileOpFlush
- Flush any cached data to storage.
- ARGUMENTS:
-
stream
= open stream handle -
valueP
=NULL
-
valueLenP
=NULL
- RETURNS:
- zero on success;
fileErr
... on error;
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileClearerr()
, FileEOF()
, FileError()
, FileFlush()
, FileGetLastError()
, FileRewind()
FileDelete Function
Purpose
Deletes the specified file stream from the specified card. Only a closed stream may be passed to this function.
Declared In
FileStream.h
Prototype
Err FileDelete ( UInt16 cardNo, const Char *nameP )
Parameters
-
cardNo
- Card on which the file stream to delete resides. Currently, no Palm OS® devices support multiple cards, so this value must be 0.
-
nameP
- String that is the name of the stream to delete.
Returns
0 if no error, or a fileErr
code if an error occurs. See the section "File Streaming Error Codes" for more information.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileDmRead Macro
Purpose
Reads data from a file stream into a chunk, record, or resource residing in a database.
Declared In
FileStream.h
Prototype
#define FileDmRead( stream, startOfDmChunkP, destOffset, objSize, numObj, errP )
Parameters
-
→
stream
- Handle to open stream.
-
→
startOfDmChunkP
- Pointer to beginning of chunk, record or resource residing in a database.
-
destOffset
- Offset from
startOfDmChunkP
(base pointer) to the destination area (must be >= 0). -
objSize
- Size of each stream object to read.
-
numObj
- Number of stream objects to read.
-
↔
errP
- Pointer to variable that is to hold the error code returned by this function. Pass
NULL
to ignore. See the section "File Streaming Error Codes" for more information.
Returns
The number of whole objects that were read—note that the number of objects actually read may be less than the number requested.
Comments
When the number of objects actually read is less than the number requested, you may be able to determine the cause of this result by examining the return value of the errP
parameter or by calling the FileGetLastError()
function. If the cause is insufficient data in the stream to satisfy the full request, the current stream position is at end-of-file and the "end of file" indicator is set. If a non-NULL
pointer was passed as the value of the errP
parameter when the FileDmRead
function was called and an error was encountered, *errP
holds a non-zero error code when the function returns. In addition, the FileError()
and FileEOF()
functions may be used to check for I/O errors.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileRead()
, FileError()
, FileEOF()
FileEOF Function
Purpose
Get end-of-file status (err = fileErrEOF
indicates end of file condition).
Declared In
FileStream.h
Prototype
Err FileEOF ( FileHand stream )
Parameters
Returns
0 if not end of file; non-zero if end of file. See the section "File Streaming Error Codes" for more information.
Comments
This function's behavior is similar to that of the feof
function provided by the C programming language runtime library.
Use FileClearerr()
to clear the I/O error status.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileClearerr()
, FileGetLastError()
, FileRewind()
FileError Function
Purpose
Declared In
FileStream.h
Prototype
Err FileError ( FileHand stream )
Parameters
Returns
0 if no error, and non-zero if an I/O error indicator has been set for this stream. See the section "File Streaming Error Codes" for more information.
Comments
This function's behavior is similar to that of the C programming language's ferror
runtime function.
Use FileClearerr()
to clear the I/O error status.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileClearerr()
, FileGetLastError()
, FileRewind()
FileFlush Function
Purpose
Declared In
FileStream.h
Prototype
Err FileFlush ( FileHand stream )
Parameters
Returns
0 if no error, or a fileErr
code if an error occurs. See the section "File Streaming Error Codes" for more information.
Comments
It is not always necessary to call this function explicitly—certain operations flush the contents of a stream automatically; for example, streams are flushed when they are closed. Because this function's behavior is similar to that of the fflush
function provided by the C programming language runtime library, you only need to call it explicitly under circumstances similar to those in which you would call fflush
explicitly.
Compatibility
Implemented only if 3.0 New Feature Set is present.
FileGetLastError Function
Purpose
Get error code from last operation on file stream, and clear the last error code value (will not change end of file or I/O error status -- use FileClearerr()
to reset all error codes)
Declared In
FileStream.h
Prototype
Err FileGetLastError ( FileHand stream )
Parameters
Returns
Error code returned by the last file stream operation. See the section "File Streaming Error Codes" for more information.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileClearerr()
, FileEOF()
, FileError()
FileOpen Function
Purpose
Open existing file stream or create an open file stream for I/O in the mode specified by the openMode
parameter.
Declared In
FileStream.h
Prototype
FileHand FileOpen ( UInt16 cardNo, const Char *nameP, UInt32 type, UInt32 creator, UInt32 openMode, Err *errP )
Parameters
-
cardNo
- Card on which the file stream to open resides. Currently, no Palm Powered™ devices support multiple cards, so this value must be 0.
-
→
nameP
- Pointer to text string that is the name of the file stream to open or create. This value must be a valid name—no wildcards allowed, must not be
NULL
. -
type
- File type of stream to open or create. Pass
0
for wildcard, in which casesysFileTFileStream
is used if the stream needs to be created andfileModeTemporary
is not specified. If type is0
andfileModeTemporary
is specified, thensysFileTTemp
is used for the file type of the stream this function creates. -
creator
- Creator of stream to open or create. Pass
0
for wildcard, in which case the current application's creator ID is used for the creator of the stream this function creates. -
openMode
- Mode in which to open the file stream. You must specify only one primary mode selector. Additionally, you can use the
|
operator (bitwise inclusive OR) to append one or more secondary mode selectors to the primary mode selector. See "Primary Open Mode Constants" and "Secondary Open Mode Constants" for the list of possible values. -
↔
errP
- Pointer to variable that is to hold the error code returned by this function. Pass
NULL
to ignore. See the section "File Streaming Error Codes" for a list of error codes.
Returns
If successful, returns a handle to an open file stream; otherwise, returns 0
.
In some cases, on some platforms, FileOpen()
returns a non-zero value when it has failed to open a file; thus, it is always a good idea to check the errP
parameter value to determine if an error has occurred.
Comments
The fileModeReadOnly
, fileModeReadWrite
, fileModeUpdate
, and fileModeAppend
modes are mutually exclusive—pass only one of them to the FileOpen
function!
When the fileModeTemporary
open mode is used and the file type passed to FileOpen
is 0
, the FileOpen
function uses sysFileTTemp
(defined in SystemMgr.rh)
for the file type, as recommended. In future versions of Palm OS, this configuration will enable the automatic cleanup of undeleted temporary files after a system crash. Automatic post-crash cleanup is not implemented in current versions of Palm OS.
To open a file stream even if it has a different type and creator than specified, pass the fileModeAnyTypeCreator
selector as a flag in the openMode
parameter to the FileOpen()
function.
The fileModeLeaveOpen
mode is an esoteric option that most applications should not use. It may be useful for a library that needs to open a stream from the current application's context and keep it open even after the current application quits. By default, Palm OS automatically closes all databases that were opened in a particular application's context when that application quits. The fileModeLeaveOpen
option overrides this default behavior.
Compatibility
Implemented only if 3.0 New Feature Set is present.
FileRead Macro
Purpose
Reads data from a stream into a buffer. Do not use this macro to read data into a chunk, record or resource residing in a database—you must use the FileDmRead()
macro for such operations.
Declared In
FileStream.h
Prototype
#define FileRead( stream, bufP, objSize, numObj, errP )
Parameters
-
→
stream
- Handle to open stream.
-
→
bufP
- Pointer to beginning of buffer into which data is read
-
objSize
- Size of each stream object to read.
-
numObj
- Number of stream objects to read.
-
↔
errP
- Pointer to variable that is to hold the error code returned by this function. Pass
NULL
to ignore. See the section "File Streaming Error Codes" for a list of error codes.
Returns
The number of whole objects that were read—note that the number of objects actually read may be less than the number requested.
Comments
Do not use this macro to read data into a chunk, record or resource residing in a database—you must use the FileDmRead()
macro for such operations.
When the number of objects actually read is fewer than the number requested, you may be able to determine the cause of this result by examining the return value of the errP
parameter or by calling the FileGetLastError()
function. If the cause is insufficient data in the stream to satisfy the full request, the current stream position is at end-of-file and the "end of file" indicator is set. If a non-NULL
pointer was passed as the value of the errP
parameter when the FileRead
function was called and an error was encountered, *errP
holds a non-zero error code when the function returns. In addition, the FileError()
and FileEOF()
functions may be used to check for I/O errors.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileRewind Function
Purpose
Reset position marker to beginning of stream and clear all error codes.
Declared In
FileStream.h
Prototype
Err FileRewind ( FileHand stream )
Parameters
Returns
0 if no error, or a fileErr
code if an error occurs. See the section "File Streaming Error Codes" for more information.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileSeek()
, FileTell()
, FileClearerr()
, FileEOF()
, FileError()
, FileGetLastError()
FileSeek Function
Purpose
Set current position within a file stream, extending the stream as necessary if it was opened with write access.
Declared In
FileStream.h
Prototype
Err FileSeek ( FileHand stream, Int32 offset, FileOriginEnum origin )
Parameters
-
→
stream
- Handle to open stream.
-
offset
- Position to set, expressed as the number of bytes from origin. This value may be positive, negative, or
0
. -
origin
- Describes the origin of the position change. Possible values are:
Returns
0 if no error, or a fileErr
code if an error occurs. See the section "File Streaming Error Codes" for more information.
Comments
Attempting to seek beyond end-of-file in a read-only stream results in an I/O error.
This function's behavior is similar to that of the fseek
function provided by the C programming language runtime library.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileTell Function
Purpose
Retrieves the current position and, optionally, file size, of a stream.
Declared In
FileStream.h
Prototype
Int32 FileTell ( FileHand stream, Int32 *fileSizeP, Err *errP )
Parameters
-
→
stream
- Handle to open stream.
-
↔
fileSizeP
- Pointer to variable that holds value describing size of stream in bytes when this function returns. Pass
NULL
to ignore. -
↔
errP
- Pointer to variable that is to hold the error code returned by this function. Pass
NULL
to ignore. See the section "File Streaming Error Codes" for a list of possible error codes.
Returns
If successful, returns current position, expressed as an offset in bytes from the beginning of the stream. If an error was encountered, returns -1
as a signed long integer.
Comments
The FileTell
function can return the size of the input stream; as such, it provides some of the functionality of the standard C library stat
function. Note, however, that unlike the stat
function, FileTell
requires that the file be open.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileTruncate Function
Purpose
Truncate the file stream to a specified size; not allowed on streams open in destructive read mode or read-only mode.
Declared In
FileStream.h
Prototype
Err FileTruncate ( FileHand stream, Int32 newSize )
Parameters
Returns
0 if no error, or a fileErr
code if an error occurs. See the section "File Streaming Error Codes" for a list of possible error codes.
Compatibility
Implemented only if 3.0 New Feature Set is present.
See Also
FileWrite Function
Purpose
Declared In
FileStream.h
Prototype
Int32 FileWrite ( FileHand stream, const void *dataP, Int32 objSize, Int32 numObj, Err *errP )
Parameters
-
→
stream
- Handle to open stream.
-
→
dataP
- Pointer to buffer holding data to write.
-
objSize
- Size of each stream object to write; must be
0
. -
numObj
- Number of stream objects to write.
-
↔
errP
- Optional pointer to variable that holds the error code returned by this function. Pass
NULL
to ignore. See the section "File Streaming Error Codes" for a list of possible error codes.
Returns
The number of whole objects that were written—note that the number of objects actually written may be less than the number requested. Should available storage be insufficient to satisfy the entire request, as much of the requested data as possible is written to the stream, which may result in the last object in the stream being incomplete.
Comments
Writing to files opened without write access or those that are in destructive read state is not allowed; thus, you cannot call the FileWrite()
, FileSeek()
, or FileTruncate()
functions on a stream that is in destructive read mode. One exception to this rule applies to streams that were opened in "write + append" mode and then switched into destructive read state. In this case, the FileWrite
function can append data to the stream, but it also preserves the current stream position so that subsequent reads pick up where they left off (you can think of this as a pseudo-pipe).
Compatibility
Implemented only if 3.0 New Feature Set is present.
File Streaming Error Codes
This section lists all error codes returned by the file streaming functions.