Format and Use of jdeFopen() API Function for Text Files
Purpose of Document
Currently the jdeFopen() API enables you to read a text file or write a text file. This document provides guidance on how to use the API and what is the proper follow-up implementation to prevent memory leaks.
Currently jdeFopen() is written based on Unicode as below,
Used For: Open file named by Unicode strings. (This is a wrapper function that takes Unicode strings, but the actual physical file name may be using other encoding).
NOTE: The API jdeFopen() can be used to read existing files or to write to a target file.
Available format (refer to C++ standard)
Format
Explanation
Others
a
Append
In writing target file
r
Read
In reading source file
w
overWrite
Opens an empty file for writing. If the given file exists, its contents are destroyed.
a+b
open in Binary format for Appending
Open it in Binary Format in AS400 to append additional text into exiting file
rb
open in Binary format for Reading
(JDENV_AS400)
w+b
open in Binary format and truncate/overWrite
In AS400 when you want to truncate the existing data and write it with new ones
a+
Read and Append
Open for reading and appending
w+
overWrite
Opens an empty file for both reading and writing. If the file exists, its contents are destroyed.
wb
overWrite in Binary
Opens an empty file for writing in binary. If the file exists, its contents are destroyed.
at
Append Text
Open and apppend in text (translated) mode.
Example of implementation (B34A1010 - OpenFlatFile (Open Flat File))
Code example,
if ( IsStringBlank(lpDS->szFilePath) ) /* Check whether input FilePath is Blank or not */ { /* Set Error */ } else { if ( lpDS->cAppendMode == _J('1') ) /* Depends on input value */ { fpOutFile = jdeFopen(lpDS->szFilePath, _J("a+b")); /* Append in Binary */ } else { fpOutFile = jdeFopen(lpDS->szFilePath, _J("wb")); /* Overwrite in Binary */ }
if ( !fpOutFile ) { /* Set Error when File Pointer is not True */ } else { lpDS->idFilePtr = jdeStoreDataPtr(hUser, fpOutFile); /* Store Pointer */ } }
Proper follow-up Action to take:
Check whether file pointer is NULL or not
If not NULL then call jdeFclose()
Remove Data Pointer
Set NULL for File Pointer
fpOutFile = jdeRetrieveDataPtr(hUser, lpDS->idFilePtr); /* Get Data Pointer */ if (fpOutFile) /* File Pointer is NOT NULL */ { jdeFclose(fpOutFile); /* Close File */ jdeRemoveDataPtr(hUser, (unsigned long)lpDS->idFilePtr); /* Remove Data Pointer */ lpDS->idFilePtr = 0L; /* NULL */ }