[Gambas-user] EOF problem

Doriano Blengino doriano.blengino at ...1909...
Sun Apr 18 22:50:37 CEST 2010


Les Hardy ha scritto:
> Doriano Blengino wrote:
>   
>> Les Hardy ha scritto:
>>   
>>     
>>> Hi Ed,
>>> I figured it out.
>>> First of all, depending how the text file was emptied/created, the 
>>> contents may not be truely empty.
>>> Gedit for example leaves the 0A (end-of-line) character in the 'empty' 
>>> file. This is recognised by eof() as a character, so, end-of-file is not 
>>> found.
>>> I am not sure if this can be considered a bug in Gambas, but it does 
>>> mean an eof() is only good for preventing read errors, and is not 
>>> reliable for testing if a file is empty.
>>>   
>>>     
>>>       
>> Just breaking in to say that a file with a EOL in it is not an empty 
>> file - it is a file with an empty line in it...
>>
>>     
> Your correct, I agree completely, but the original code Ed supplied, ' 
> IF Eof(tfile) ' cannot work , as it checks eof before a read.
> When I saw he was using eof() that way, and knowing empty files are not 
> always empty, I thought I had found his problem, so I just added a bit 
> of code that should have got around it.
>
> I am sure you will agree, that, whatever other problems he may have,  he 
> needs not to use eof() the way he is.
>   
Here is the original code. I think it is perfectly right:
> PUBLIC SUB Button1_Click()
>
>    DIM tfile AS File
>    DIM Fname AS String
>
>    Fname = "/home/us/Documents/testdat.txt"
>
>    tfile = OPEN Fname FOR READ
>
>    label1.Text = ""
>
>    IF Eof(tfile) THEN
>       label1.Text = "EOF"
>    ELSE
>       LINE INPUT #tfile, label1.Text
>    END IF
>
>    CLOSE #tfile 
>
> END
>
>   
If eof() is not checked *before* reading, then when? I see only two 
things I would not do (but it depends a lot). First, why use the "fname" 
variable, if it is used only once... second, why set label1.text if that 
.text will be anyway written shortly later?

But, apart from these two very little things, that can also have some 
good reason, the rest of the code is the only possible, I think. Note 
also that label1.text can get 3 different values: "EOF" is the file is 
empty; "" if the first line of the file is empty; something else if the 
first line of the file contains some data. These three cases cover all 
the cases we talked about (empty and not empty files), and I think our 
friends know what they are doing.

Of course I may be wrong, but that use of eof() seems to me fully ok.

Regards,

-- 
Doriano Blengino

"Listen twice before you speak.
This is why we have two ears, but only one mouth."





More information about the User mailing list