file i/o in a variety of languages

Steven Schveighoffer schveiguy at yahoo.com
Fri Aug 27 13:48:18 PDT 2010


On Fri, 27 Aug 2010 16:41:58 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 8/27/10 13:28 PDT, Steven Schveighoffer wrote:
>> 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.

BTW, Don pointed out the clarifcation that I missed.  It actually is  
correct, my apologies.

>> 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);

Hm.. this still allocates.  We can do better than the C++ example:

char[128] buf;
char[] line = buf[];
f.readln(line);
f.readln(line);

Which should not allocate at all in this case, and is completely safe if  
it *does* have to allocate (like if some malicious code came along and  
rewrote the file to have a 500-character first line).  Try to do *that*  
with std::string :)

Man D is just so cool!

-Steve


More information about the Digitalmars-d mailing list