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?
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.
You can check if the file already exists and if it does not exist you can put the default values into the file, which will then be created.
See below
if there is not a file "mysettings.dat"
then put myDefaultsettings into URL "binfile:mysettings.dat"
Related
Let's say I have multiple files. I want to loop through all of them and always read 2 files: previous one and the current one.
I do not want to read files simultaneously: read the previous one in respect to the current file.
So first I read the first file (well, this one is an exception since the previous file does not exist for it). Then 1st and 2nd. Then 2nd and 3rd. Then 3rd and 4th and so on.
You can try something like
prev = None
for fpath in files_iter:
if prev is not None:
"<read previous>"
"<read current>"
prev = fpath
I'm writing a script that converts a Markdown file to a PDF, facilitated by Pandoc.
So if you drag C:\Users\User\Documents\English\PAPER1.md onto the script, it'll create C:\Users\User\Documents\English\PDFs\PAPER1.pdf.
This is achieved in the header of the script via
set INPUT=%1
set PDFDIR=%~dp1\PDFs
set PDF=%PDFDIR%\%~n1.pdf
However, in certain circumstances, the input filename will be Something.md.txt, in which case I only still want to output Something.pdf. (Removing more than three extensions will probably not be necessary or desirable.)
But the current setup only strips one extension, producing Something.md.pdf.
However, %~nn1.pdf does not work, nor does set p=%~n1 set PDF=%~np, giving
The following usage of the path operator in batch-parameter
substitution is invalid: %~np
How do I get the bare filename of a file with "two extensions"?
I'd change every line from the provided header of the script to:
Set "INPUT=%~1"
Set "PDFDIR=%~dp1PDFs"
For %%A In ("%~dpn1") Do Set "PDF=%PDFDIR%\%%~nA.pdf"
Because there's no surety of the input content, please use best practice and always reference these variables wherever possible using doublequotes:
Echo "%INPUT%"
Echo "%PDFDIR%"
Echo "%PDF%"
i am using mergext dropboxsync to synchronise my data from the ipad.
my question is.
how we get the file from the special folder specialfolderpath
and how we check if the file exist into the dropbox.
i am no using fields for folder or the files i want the procedure to be hidden from the user
to get my folder and path i use the simple bellow code.
put specialfolderpath("documents") & "/myfile.sqlite" into myPath
and here is the code from the button i use
on mouseUp
goToParent --<command
repeat for each line tempitem in fld "sFolders" --<hide field
add 1 to t
if tempitem ="hairaid-backup" then
put 1 into fExist
end if
end repeat
--!! if folder exist
if fExist is a number then
else
try
mergDropboxCreateFolder (hairaid &"-"&backup)
catch e
answer e
end try
end if
end mouseUp
I would not really recommend synchronising a sqlite database over dropbox. You are likely to have better results creating a tree for files for dropbox to sync as one large file is likely to end up with conflicted versions.
However, answering your question in the general sense you would need to do something like this:
put url ("binfile:"&myPath) into myData
mergDropboxWriteFile relativePath,myData
Then to get the data you:
put merDropboxGetFile(relativePath) into myData
As part of moving from a windows server to a linux server I have to clean up a large number of filenames.
My Problem is that when I execute:
db_query("UPDATE {files} SET filename = '%s' AND filepath = '%s' WHERE fid = %d", $file->filename, $file->filepath, $file->fid);
and afterwards select the content for $file->fid the filename field has the value of "0"
If I dump the query as text both before and after it's being executed the filename field contains the filename I have specified where as the filepath is being stored correctly.
DAMN! putting an AND into an update query will not produce the expected result... MySQL allows this but it's not the way to go :)
Use a comma instead of AND.
Might also want to look into using drupal_write_record() instead of db_query. drupal_write_record will automatically update a pre-existing row if you add the 3rd parameter for a key to check. In your case, you could use the file id.
I am creating several text folders programmatically using VB6, and then concatenating them all together into a single file.
I write text to the files using
Print #lngFileHandle, Text
so there should be a CR/LF even after the very last line of text in each file.
Then I append all these "subfiles" together into another text file that was opened this way:
Open strFileName For Append As #lngFileHandle
Strangely, my final resulting file looks good EXCEPT that the very last line of the last file being appended is only partially there.
The last few lines look like this in the file I'm reading FROM:
`<Name>` Referral for Service Home Delivered Meals`</Name>`
`<Name>` Referral for Service Adult Day Care/Health`</Name>`
`<Name>` Referral for Service Congregate Meals`</Name>`
but after being read in from that file and output to the final file, they look like this:
`<Name>` Referral for Service Home Delivered Meals`</Name>`
`<Name>` Referral for Service Adult Day Care/Health`</Name>`
`<Name>` Referral for Service Congr
The code I'm using to read in this particular "subfile" and output it to the final file is:
With mobjNewEntriesLog
Do While Not .IsEOF
strOutput = .ReadLine
mobjMainLog.PrintLine strOutput
Loop
End With
The .IsEOF function is as follows:
Public Function IsEOF() As Boolean
If blnOpened Then
IsEOF = EOF(lngFileHandle)
Else
IsEOF = True
End If
End Function
It would make more sense to me if I wasn't getting the last line at ALL, but getting just PART of it?--I don't get that.
Anybody see anything that would make the last line only print partially to the final file?
TIA.
Ensure you are closing your file as this may be required to flush out any data that is pending to be written.
VB6 file numbers are not file handles, so don't call them that. They are indexes into a file descriptor table in the runtime where the actual handle, mode, buffer length, buffer, ponters, etc. are stored.
The Close statement is not synchronous, but a "lazy close" that may not have flushed all data and updated the EOF pointer of the file by the time you turn around and try to read it again. This behavior is intentional as far as I can determine, perhaps for performance reasons.
A Reset statement can be used to force all open files closed, and it is synchronous. This isn't always practical, however it may be fine in your case. Easy enough to try: add a Reset before you re-open any of your files to concatenate them.