Posts: 43
Threads: 13
Joined: Nov 2002
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.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Try adding a
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 "".
Posts: 6,419
Threads: 74
Joined: Mar 2002
Try adding a
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 "".
Posts: 3,368
Threads: 195
Joined: Jan 2003
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.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 43
Threads: 13
Joined: Nov 2002
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.
Posts: 3,368
Threads: 195
Joined: Jan 2003
You don't need to define the length of the string in the TYPE clause. You don't need a TYPE clause.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 43
Threads: 13
Joined: Nov 2002
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.
Posts: 3,368
Threads: 195
Joined: Jan 2003
Yeah, 'cuz Test is only 4 characters and the string length is 15...
So it would be:
"Test "
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 43
Threads: 13
Joined: Nov 2002
Yeah, it made perfect sense after I thought about afterwards. Again, thanks for the help.
Thanks also, Na_th_an for your help.