I want to save text to a file. Is there a way be notified or get a return value whether the file was successfully saved or not?
StringToFile() has no return value and OpenFile() with save mode also doesn't seem to help as I "just" get the id (when the file could be opened) but not the result of the writing operations.
Thanks!
Get success/error on file save
Get success/error on file save
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
Re: Get success/error on file save
Just a thought, could it be an option to use fileattributes to check the time the file was last modified, and check again after writing and compare the result?
If the time of modification have changed, it should indicate that the file have been updated succesfully.
If the time of modification have changed, it should indicate that the file have been updated succesfully.
Re: Get success/error on file save
This does seem to work
Note that the last fileattribute command could have been done just before the previous closefile, avoiding the need to open the file again.
I just did it this way to have a separate section to make the example more clear.
By the way.
Andreas, if you are reading this
The example in the docs for fileattributes needs correction
This is copied from the docs
It expects a number as argument, so it must be like this to work:
Note that the last fileattribute command could have been done just before the previous closefile, avoiding the need to open the file again.
I just did it this way to have a separate section to make the example more clear.
Code: Select all
;creates an empty file in case it doesnt exist
If Not Exists("save/inventory.data")
StringToFile("", "save/inventory.data")
wait(50) ; add a delay just to make sure the timecheck will work
EndIf
;open the file in readmode to check the modified date
OpenFile(1, "save/inventory.data", #MODE_READ)
old = FileAttributes(1)
CloseFile(1)
; check that the file is not protected before attempting to write
If old.flags & #FILEATTR_READONLY
DebugPrint("File is READONLY")
Else
OpenFile(1, "save/inventory.data", #MODE_WRITE)
For i = 0 To ListItems(inv$) -1
WriteLine(1, inv$[i])
Next
CloseFile(1)
EndIf
;Open the file again to check the time of modification
OpenFile(1, "save/inventory.data", #MODE_READ)
new = FileAttributes(1)
CloseFile(1)
If new.time > old.time
DebugPrint("SUCCESS")
DebugPrint("Before: "..old.time.." After: "..new.time)
Else
DebugPrint("File not updated")
EndIf
Andreas, if you are reading this
The example in the docs for fileattributes needs correction
This is copied from the docs
Code: Select all
OpenFile(1, "test.txt")
t = FileAttributes("test.txt")
Print(t.time)
If t.flags & #FILEATTR_READ_USR
Print("#FILEATTR_READ_USR is set.")
Else
Print("#FILEATTR_READ_USR is not set.")
EndIf
Code: Select all
OpenFile(1, "test.txt")
t = FileAttributes(1)
Print(t.time)
If t.flags & #FILEATTR_READ_USR
Print("#FILEATTR_READ_USR is set.")
Else
Print("#FILEATTR_READ_USR is not set.")
EndIf
Re: Get success/error on file save
I guess Hollywood gives an error if a write doesn't succeed, and exits (quits) the program unless you handle it otherwise.
So, if you want to check it, at least one way to do it is to disable internal error handler and check yourself.
Something like this:
I think that should be enough for all cases, but if you want to double check, you could compare the data and file sizes, like this:
So, if you want to check it, at least one way to do it is to disable internal error handler and check yourself.
Something like this:
Code: Select all
s$="blabla"
f$="ram:bla.txt"
ExitOnError(False)
StringToFile(s$, f$)
err=GetLastError()
ExitOnError(True)
If err
err$=GetErrorName(err)
DebugPrint("Write failed. Reason:", err$)
EndIf
Code: Select all
If ByteLen(s$)<>FileSize(f$)
DebugPrint("Written size differs for some weird reason?!")
EndIf
Re: Get success/error on file save
Thanks a lot for your suggestions! I also thought about the file date attribute. jPV's solution is also very need and probably more fitting.
Currently I am on vacation so I probably cannot test your solution and built it in my poject but will do afterwards. Also sorry for the late reply (also because of vacation).
Currently I am on vacation so I probably cannot test your solution and built it in my poject but will do afterwards. Also sorry for the late reply (also because of vacation).
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
- airsoftsoftwair
- Posts: 5433
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Get success/error on file save
Thanks, fixed.
@all:
The easiest way to do this is to use Hollywood's new ?-syntax, so instead of the very cumbersome ExitOnError(False), call, GetLastError(), ExitOnError(True) route you could just do:
Code: Select all
If ?StringToFile("blabla", "ram:blabla.txt")
DebugPrint("Error!")
EndIf
Re: Get success/error on file save
This is wonderful, thanks, Andreas!
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc