file i/o in a variety of languages

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Aug 27 13:41:58 PDT 2010


On 8/27/10 13:28 PDT, Steven Schveighoffer wrote:
> On Fri, 27 Aug 2010 16:18:26 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> On 8/27/10 13:01 PDT, Steven Schveighoffer wrote:
>>> On Fri, 27 Aug 2010 05:36:32 -0400, Andrei Alexandrescu
>>> <SeeWebsiteForEmail at erdani.org> wrote:
>>>
>>>> D doesn't look half bad:
>>>>
>>>> http://stackoverflow.com/questions/3538156/file-i-o-in-every-programming-language-closed
>>>>
>>>>
>>>
>>>
>>> Note that D's version doesn't follow the rules, you were supposed to
>>> re-open the file for appending, they just never closed it.
>>
>> The call to open() closes the file (and throws on error) and then
>> opens the file (and throws on error). I think that's fair.
>
> No, the code does this:
>
> f.writeln("hello");
> f.writeln("world");
>
> The example is supposed to demonstrate how to re-open the file for
> appending and write "world". Look at some of the other examples. Not
> that it's a big deal, because I think it's just one more line.

Oh, I understand now. Thanks.

>>> It also reads the first line into a buffer and then throws it away. Does
>>> File have a way to skip a line?
>>
>> Not that I know of. Save for the allocation, the effort is the same -
>> the implementation still has to look for the '\n'.
>
> Allocation can be expensive. With D-allocated buffers, this would be
> trivial to do without allocation, just keep refilling the buffer until
> you find a '\n'. But searching in the FILE * buffer is not trivial at
> all. I think D is going to eventually have to address this.
>
> I bring it up because people look at the C++ or C version and say "how
> ugly, look how nice D looks," but the C++ version doesn't incur extra
> allocations AFAIK. It's like commenting on how beautiful function qsort
> looks. In reality, it's not as bad, because it's just that the
> functionality isn't there yet. If it were, it would still look as
> beautiful :) I just hate it when people compares an apple to orange and
> comment on how the orange looks like a much better apple.

I agree. In fairness, the same goes about comparing incorrect code with 
correct code. My understanding is that quite a few examples given in 
that thread are not correct, in spite of looking quite elaborate.

FWIW it's not much aggravation to avoid unnecessary allocations:

char[] line;
f.readln(line);
f.readln(line);


Andrei


More information about the Digitalmars-d mailing list