Qbasicnews.com

Full Version: Random Access files/IF
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I try to compare a string variable that is also a random-file record against a regular string variable, the IF statement is completely ignored, and just moves on to the ELSE statement.

For example:
Quote:TYPE samplerecord
var1 as string*15
END TYPE

DIM record as samplerecord

OPEN "test.dat" for RANDOM as #1 LEN=LEN(record)

'write a sample value to the string for an example

record.var1="Test"
'Write the variable to the file and then retrieve it
PUT #1, 1, record
GET #1, 1, record

IF record.var1="Test" THEN 'test the variable
PRINT "Ok."
ELSE
PRINT "Did not work."
ENDIF

'as you can see it won't work.

Thanks for any help, once again. Big Grin
Try adding a

Code:
SEEK #1,1

Between the PUT and the GET.

What is happening is that after the PUT the pointer inside the file is located at the end of file, and when you GET you get from there and there is nothing, so it returns "".
Try adding a

Code:
SEEK #1,1

Between the PUT and the GET.

What is happening is that after the PUT the pointer inside the file is located at the end of file, and when you GET you get from there and there is nothing, so it returns "".
The QB Help example for GET and PUT is very crummy, as it uses a TYPE and at first glance there is no way to get around it.

What you need to know is that the length of the string must be specified. If it's blank, just fill it with spaces and then get the result.

Code:
record$="Test"
OPEN "test.dat" for RANDOM as #1
PUT #1, 1, record$
GET #1, 1, record$
CLOSE
PRINT record$

Prints "Test" as expected.
Na_th_an, SEEK did not seem to work.

Agamemnus, I do not exactly understand what you are saying. I have the length of the string defined in the TYPE clause.
You don't need to define the length of the string in the TYPE clause. You don't need a TYPE clause.
As is usually the case with my problems, I seem to have fixed it. In the IF statement I put the RTRIM$ command over the record variable to remove any spaces.

Thanks for your help which led me to this solution.
Yeah, 'cuz Test is only 4 characters and the string length is 15...

So it would be:

"Test "
Yeah, it made perfect sense after I thought about afterwards. Again, thanks for the help. Smile

Thanks also, Na_th_an for your help. Smile