Test to see if a file already exists

MadPink

I have an iOS game, I am trying to save settings (which are generally stored within an array) to file.

currently,I have the file opened and read in the openStack handler; I have the file written in the shutdown handler...

but, in the openStack handler, how do I test to see if the file has actually been created... and if it doesn't exist I want to create one and write in some default settings

What's the best way to do this?

Mark

Usually, I just open and read the file. Next, I put the contents into variables. If the contents happens to be empty, then I use a default value. This makes it unnecessary to check that the file exists and, more importantly, is more compatible with future versions of your software.

on readPrefs
  put specialFolderpath("documents") & "/prefs.dat" into myPath
  put url ("binfile:" & myPath) into myPrefs
  // here, the result may contain "can't open file"
  put line 1 of myPrefs into gHighscore
  if gHighScore is empty then put 0 into gHighscore
  put line 2 of myPrefs into gLicenseKey
  if gLicenseKey is empty then put "unregistered" into gLicenseKey
end readPrefs

You could also check for the file and use a slightly different script:

on readPrefs
  put specialFolderpath("documents") & "/prefs.dat" into myPath
  if there is a file myPath then
    put url ("binfile:" & myPath) into myPrefs
    put line 1 of myPrefs into gHighscore
    put line 2 of myPrefs into gLicenseKey
  end if
  if gHighScore is empty then put 0 into gHighscore
  if gLicenseKey is empty then put "unregistered" into gLicenseKey
end readPrefs

More variations are possible, e.g. you could check the result and set default values if the file can't be opened.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

If File Already Exists Rename it

From Dev

Error in file already exists

From Dev

How to test if a table already exists?

From Dev

Check to see if an Outlook TaskList Item already exists

From Dev

Checking if destination file already exists

From Dev

Rename a file if same already exists

From Dev

Checking if destination file already exists

From Dev

Rename a file if same already exists

From Dev

Test to see if variable exists, then end the for loop

From Dev

Regex, test to see if characters other than exists

From Dev

File.Copy() performance if a file already exists

From Dev

Renaming a file and appending a digit if a file already exists

From Dev

File Already Exists When Moving File

From Dev

Renaming a file to name of file that already exists

From Dev

How do I check to see if a user exists already in the controller?

From Dev

How to see if a delphi component already exists in your application? -delphi

From Dev

mySQL + vba, checking to see if an item already exists in a table

From Dev

How to copy-paste a file that already exists?

From Dev

Append text to text file if it already exists

From Dev

How to check if the file already exists in the LinkItemCollection?

From Dev

Ember-CLI error: file already exists

From Dev

Save with a different name if the file already exists in directory

From Dev

Move and replace if same file name already exists?

From Dev

MapReduce on Hadoop says 'Output file already exists'

From Dev

powershell update file in subfolders if it already exists

From Dev

How to check if file name already exists?

From Dev

Copy directory, but fail if file already exists at destination

From Dev

Unable to set as Ringtone because file already exists

From Dev

The file name you submitted already exists on the server

Related Related

  1. 1

    If File Already Exists Rename it

  2. 2

    Error in file already exists

  3. 3

    How to test if a table already exists?

  4. 4

    Check to see if an Outlook TaskList Item already exists

  5. 5

    Checking if destination file already exists

  6. 6

    Rename a file if same already exists

  7. 7

    Checking if destination file already exists

  8. 8

    Rename a file if same already exists

  9. 9

    Test to see if variable exists, then end the for loop

  10. 10

    Regex, test to see if characters other than exists

  11. 11

    File.Copy() performance if a file already exists

  12. 12

    Renaming a file and appending a digit if a file already exists

  13. 13

    File Already Exists When Moving File

  14. 14

    Renaming a file to name of file that already exists

  15. 15

    How do I check to see if a user exists already in the controller?

  16. 16

    How to see if a delphi component already exists in your application? -delphi

  17. 17

    mySQL + vba, checking to see if an item already exists in a table

  18. 18

    How to copy-paste a file that already exists?

  19. 19

    Append text to text file if it already exists

  20. 20

    How to check if the file already exists in the LinkItemCollection?

  21. 21

    Ember-CLI error: file already exists

  22. 22

    Save with a different name if the file already exists in directory

  23. 23

    Move and replace if same file name already exists?

  24. 24

    MapReduce on Hadoop says 'Output file already exists'

  25. 25

    powershell update file in subfolders if it already exists

  26. 26

    How to check if file name already exists?

  27. 27

    Copy directory, but fail if file already exists at destination

  28. 28

    Unable to set as Ringtone because file already exists

  29. 29

    The file name you submitted already exists on the server

HotTag

Archive