Resources | Resources |



Reading and writing files using IFilePort1

To create a file, and then write to and read from the file, an application needs to do the following:

  1. Call ISHELL_CreateInstance() to obtain an instance of the IFileSystem2 Interface.
  2. Call IFileSytem2_Open() to create the file, supplying the filename as input.

    After creation, the file is open for reading and writing.

  3. Using the IFilePort1 pointer obtained in step 2, call IFilePort1_Seek() if necessary to position the file cursor, and then call IFilePort1_Write() and IFilePort1_Read() to write to and read from the file as needed.
  4. Call IFilePort1_Release() to close the file.
  5. If the file is no longer needed, call IFileSystem2_Unlink() to delete the file.

The examples below show how these steps are done.

Create a file

This example creates a file, then calls IFilePort1_Stat() to get file information.

#include ""
#include "AEEIFileSystem2.h"
#include "AEEIFilePort1.h"
#include "AEEFSmode.h"

#define FS_APP_CLASSID "fs:/~0x0100900F/"

   IFilePort1 * pIFilePort1 =NULL;
   IFileSystem2 * pIFileSystem2=NULL;
   int bufLenReq;
   int64 nOutOffset;
   int written;
   int nRet;

   char szFirstStr[]  = "First Sentence";
   char szSecondStr[] = "Second Sentence";
   char szThirdStr[]  = "Third Sentence";
   char szBuffer[30];

   if(nRet=ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_FileSystem2,
             (void **)&pIFileSystem2)){
       DBGPRINTF("%d=create(AEECLSID_FileSystem2)", nRet);
       goto bail;

   // Create the file xxyy.zz      
   if (!(nRet = IFileSystem2_Open(pIFileSystem2, FS_APP_CLASSID "xxyy.zz", 
             FS_CAP_CREATE FS_CAP_RDWR, &pIFilePort1 )) ) {
      FSStatEx fsstatex;
      JulianType Date;
      // To verify that we opened the file successfully, use
      // the file pointer to retrieve its creation date
     if(nRet=IFilePort1_Stat(pIFilePort1, &fsstatex )){
        DBGPRINTF("%d=IFilePort1_Stat()", nRet);
        goto bail;
     GETJULIANDATE(fsstatex.dwCreationDate,&Date );
     DBGPRINTF("Created: %d/%d/%d %d:%d:%d", 
              Date.wMonth, Date.wDay ,Date.wYear,
              Date.wHour,Date.wMinute, Date.wSecond);     

Write to the file

   // Write three sentences to the file
   if (nRet=IFilePort1_Write(pIFilePort1, szFirstStr,
            sizeof(szFirstStr),&written)) {
      DBGPRINTF("%d=IFilePort1_Write()", nRet);
   if (nRet=IFilePort1_Write(pIFilePort1, szSecondStr,
            sizeof(szSecondStr),&written)) {
      DBGPRINTF("%d=IFilePort1_Write()", nRet);
   if (nRet=IFilePort1_Write(pIFilePort1, szThirdStr,
            sizeof(szThirdStr),&written)) {
      DBGPRINTF("%d=IFilePort1_Write()", nRet);

Read from the file

This example reads the content that was written in the previous example. Before reading from the file, the file pointer must be moved back to the beginning of the file by calling IFilePort1_Seek(). Each time IFilePort1_Read() is called, the file pointer is automatically moved to the end of the content that was read.

   // Move the file pointer to the beginning of the file
   if (nRet=IFilePort1_Seek(pIFilePort1, 0, 0, &nOutOffset)) {
      DBGPRINTF("%d=IFilePort1_Seek()", nRet);

   // Read each sentence and echo it on the screen.
   // The read function returns the number of bytes read,
   // which is checked to make sure the read was successful.
   if (nRet= IFilePort1_Read(pIFilePort1, szBuffer,
            sizeof(szFirstStr),&bufLenReq)) {
      DBGPRINTF("%d=IFilePort1_Read()", nRet);
   if (bufLenReq == sizeof(szFirstStr)){

   if (nRet= IFilePort1_Read(pIFilePort1, szBuffer,
            sizeof(szSecondStr),&bufLenReq)) {
      DBGPRINTF("%d=IFilePort1_Read()", nRet);
   if (bufLenReq == sizeof(szSecondStr)){

   if (nRet= IFilePort1_Read(pIFilePort1, szBuffer,
            sizeof(szThirdStr),&bufLenReq)) {
      DBGPRINTF("%d=IFilePort1_Read()", nRet);
   if (bufLenReq == sizeof(szThirdStr)){

Close the file

   // Close the file

   // Remove the file, if it is no longer needed
   if (nRet = IFileSystem2_Unlink(pIFileSystem2,
            FS_APP_CLASSID "xxyy.zz")){
      DBGPRINTF("%d=IFileSystem2_Unlink()", nRet);

   if (pIFileSystem2){
   if (pIFilePort1){