Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Sat Nov 18, 2017 10:21 am

All times are UTC - 7 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: File handling
PostPosted: Tue Dec 09, 2008 10:08 am 
Offline
User avatar

Joined: Fri Feb 01, 2008 4:50 pm
Posts: 101
I would like to open a particular file, and have it created if it doesn't exist, and written to if it does not contain any data. I open the file and create it if it isn't there, which works, but when reading I get an error (#228). So, I then try to write to it, but that doesn't seem to work. What am I not getting here? here is the code (parts removed for brevity):

Code:
        dim fileD as filedesc
        dim passwd as string
        fileD = openFile(file, FILE_READ and FILE_WRITE and FILE_TEXT)
 
        check error
            readfile(fileD, passwd)
        on error
            writefile(fileD, "user pass\n")
            closeFile(fileD)
            return
        enderr


And the exception:

Code:
Exception 228: ReadFile: End of file reached without sufficient data (severity: 5)


Thanks!


Top
 Profile  
 
 Post subject: Re: File handling
PostPosted: Tue Dec 09, 2008 10:23 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Code:
func test()
    dim fileD as filedesc
    dim passwd as string

    'In general I find it easier to just open a file for reading or writing but not both
    check error
        fileD = openFile(file, FILE_READ and FILE_TEXT)
    on error
        'Removes the exception from the queue.  Otherwise the green screen will complain
        _ClearException()

        'Create a stub file
        fileD = OpenFile(file, FILE_WRITE and FILE_TEXT)
        CloseFile(fileD)
        fileD = OpenFile(file, FILE_READ and FILE_TEXT)
    enderr

    check error
        readfile(fileD, passwd)
        CloseFile(fileD)
    on error
        _ClearException()

        'Implies that the file does not contail the password
        CloseFile(fileD)
        'Note: This call will destroy any existing contents in the file (i.e. if the file's
        '      first entry was not a string but a number)
        fileD = OpenFile(file, FILE_WRITE and FILE_TEXT)
        WriteFile(fileD, "user pass\n")
        CloseFile(fileD)
        passwd = "user pass\n"
    enderr
endfunc


Note: Text files in Qlarity are really a form of quasi-record based files. This means that the string you write is saved as a single record and you probably don't want to put a \n after it, unless you really want a newline in the password.


Top
 Profile  
 
 Post subject: Re: File handling
PostPosted: Tue Dec 09, 2008 10:34 am 
Offline
User avatar

Joined: Fri Feb 01, 2008 4:50 pm
Posts: 101
Okay, thanks, I fixed that.

I am still getting the 228 exception though?


Top
 Profile  
 
 Post subject: Re: File handling
PostPosted: Tue Dec 09, 2008 10:36 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Do you call _ClearException in every "on error" block?

Can you duplicate this in Simulation View? If so, double click the exception message at the bottom to see which line generated it. If not, you will probably need to add "print" style debugging (_print if COM1 is free, adding messages to an EditBox if not).

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject: Re: File handling
PostPosted: Tue Dec 09, 2008 10:39 am 
Offline
User avatar

Joined: Fri Feb 01, 2008 4:50 pm
Posts: 101
I switched to BINARY mode and everything seems to work fine now.

I only call getException, but I believe this makes a call to _ClearException?

When I was reading the documentation it said that when using FILE_TEXT, a call to str(data) is made, which I imagine would end up screwing my data up anyway.


Top
 Profile  
 
 Post subject: Re: File handling
PostPosted: Tue Dec 09, 2008 10:56 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
FILE_TEXT was an unfortunate choice of names. FILE_FIELD or FILE_RECORD might have been better. WriteFile (for text mode files) will call str() on all data, but since ReadFile calls val() the data you receive from ReadFile is the data you wrote verbatim. (str() and val() are inverse operations).

FILE_BINARY will write the exact data to the file that you passed to WRITE_FILE but the drawback is that you need to know what you wrote. In the case of arrays or strings, you have to pre-size your array or string before calling readfile.

_ClearException does indeed call GetException. _ClearException in newer versions (starting in the 2.6 betas) actually is implemented in the form of

Code:
while GetException(errStr, errLev, errTyp) do
loop

to ensure that it gets all exceptions. (Some new debugging features have been added to Qlarity in 2.6 which make it more likely that a check error block will catch more than one exception -- these features are off by default but can be turned on to facilitate debugging exceptions that can only be triggered on an actual unit).

You must call GetException once per exception to prevent the green screen from showing. Internal APIs should never generate more than one exception per call. However Qlarity-written functions may generate more than one.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 7 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group