int FileCreate(char *filename);

 

Create a file.

 

Return Value

 

Returns an int handle to the file created.

 

Parameters

 

filename

Name of the file to be created.

 

Remarks

 

FileCreate() will create a new file in the Documents folder where your application resides.  These user created files can store scores, and other game information for the next time the user plays.  The Debug/Documents folder is created automatically by the SDK.

 

Availability

 

Available in DragonFireSDK 1.0 and later.

 

Example

 

This example shows how to create and manage a high scores file using each of the file functions.

 

#include "DragonFireSDK.h"

#include <string.h>

 

const int SCORE_MAX = 100; // Number of high score records to store

const int NAME_LEN = 50; // Max length of the name in the file

const char SCORES_FILE_NAME[NAME_LEN] = "Scores.dat"; // Name of the high scores file

 

// Define the high score record structure:

typedef struct

{

  char name[NAME_LEN];

  int score;

} score_record;

 

// We will keep track of the top SCORE_MAX scores:

score_record scores[SCORE_MAX];

 

// File Handle for our scores file:

int ScoresFile;

 

// Clears the entire array of score records:

void ClearScores()

{

  int i;

  for (i=0;i<SCORE_MAX;i++)

   {

       scores[i].name[0]=0;

       scores[i].score=0;

   }

}

 

// Saves the record array to a file:

void SaveScores()

{

  int i;

 

   ScoresFile=FileCreate((char *)SCORES_FILE_NAME);

  for (i=0;i<SCORE_MAX;i++)

   {

       FileWrite(ScoresFile, (char *)&scores[i], sizeof(score_record));

   }

 

   FileClose(ScoresFile);

}

 

// Inserts a new high score into the appropriate spot in the array of records:

void InsertScore(char *name,int score)

{

  int i,j;

 

  // Find the index for insertion.  Matching scores that come later will be added after existing score records:

  for (i=0;i<SCORE_MAX;i++)

   {

      if (score>scores[i].score)

          break;

   }

 

  // Start at the end of the list and shift each value down until we get to the insertion point:

  for (j=SCORE_MAX-1;j>0;j--)

   {

       strcpy(scores[j].name, scores[j-1].name);

       scores[j].score=scores[j-1].score;

      if (j==i)

          break;

   }

 

  // Insert the new score:

   strcpy(scores[i].name, name);

   scores[i].score=score;

   SaveScores();

}

 

// Creates a new save game file with names of ghosts from a popular classic video game:

void CreateDefaultScoresFile()

{

   ClearScores();

   strcpy(scores[0].name, "Inky");

   scores[0].score=1500;

   strcpy(scores[1].name, "Blinky");

   scores[1].score=1400;

   strcpy(scores[2].name, "Pinky");

   scores[2].score=1300;

   strcpy(scores[3].name, "Clyde");

   scores[3].score=1200;

   SaveScores();

}

 

void ReadScoreRecord(int index)

{

   ScoresFile=FileOpen((char *)SCORES_FILE_NAME);

  if (ScoresFile)

   {

       FileSeek(ScoresFile, (index * sizeof(score_record)));

       FileRead(ScoresFile, (char *)&scores[index], sizeof(score_record));

   }

   FileClose(ScoresFile);

}

 

// Loads the scores from the file.  Creates a new default one if none exists yet:

void LoadScores()

{

   ScoresFile=FileOpen((char *)SCORES_FILE_NAME);

  if (!ScoresFile)

   {

       CreateDefaultScoresFile();

       ScoresFile=FileOpen((char *)SCORES_FILE_NAME);

   }

   FileClose(ScoresFile);

 

  for (int i=0;i<SCORE_MAX;i++)

   {

       ReadScoreRecord(i);

   }

}

 

// Delete the scores file:

void DeleteScores()

{

  if (ScoresFile)

       FileClose(ScoresFile);

   FileDelete((char *)SCORES_FILE_NAME);

}

 

void AppMain()

{

   DeleteScores();

 

   LoadScores();

 

  // Print all of the high score records to the console:

  for (int i=0;i<SCORE_MAX;i++)

   {

       printf("High Score record #%d: NAME=%s\t\tSCORE=%d\n",i,scores[i].name,scores[i].score);

   }

 

   InsertScore("Mister Cool", 1000000);

}

 

void AppExit()

{

 

}

 

void OnTimer()

{

 

}