www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - my first D program, what is wrong with it?

reply "dominik" <aha aha.com> writes:
ok, I've just unzipped dm and dmd, and I have tried to write my first 
trivial D program - just to get started (below is the code I wrote). It 
works, from the first try :) However, it behaves em weird.
First writefln works like it is supposed to, last one also, however writef 
should just write in place haaaa and the number (hence the \r). But, when 
printing in place is done, and the last writefln fires off, line where 
writef wrote to disappears. How, why?

I'm on windows XP if that matters
----------------------------------------------
import std.stdio;

int main(char[][] args)
{

    writefln("===================");
    for (int i = 0; i < 50000; i++) {
      writef("haaaaa ", i, " \r");
    }
    writefln("===================");

    return 0;
}
---------------------------------------------- 
Sep 27 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to dominik,

 ok, I've just unzipped dm and dmd, and I have tried to write my first
 trivial D program - just to get started (below is the code I wrote).
 It
 works, from the first try :) However, it behaves em weird.
 First writefln works like it is supposed to, last one also, however
 writef
 should just write in place haaaa and the number (hence the \r). But,
 when
 printing in place is done, and the last writefln fires off, line where
 writef wrote to disappears. How, why?
 I'm on windows XP if that matters
 ----------------------------------------------
 import std.stdio;
 int main(char[][] args)
 {
 writefln("===================");
 for (int i = 0; i < 50000; i++) {
 writef("haaaaa ", i, " \r");
 }
 writefln("===================");
 return 0;
 }
 ----------------------------------------------
IIRC writefln starte outputting at the current location and appens a newline at the end. It does not ever add a newline at the start.
Sep 27 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
BCS wrote:
 Reply to dominik,
 
 ok, I've just unzipped dm and dmd, and I have tried to write my first
 trivial D program - just to get started (below is the code I wrote).
 It
 works, from the first try :) However, it behaves em weird.
 First writefln works like it is supposed to, last one also, however
 writef
 should just write in place haaaa and the number (hence the \r). But,
 when
 printing in place is done, and the last writefln fires off, line where
 writef wrote to disappears. How, why?
 I'm on windows XP if that matters
 ----------------------------------------------
 import std.stdio;
 int main(char[][] args)
 {
 writefln("===================");
 for (int i = 0; i < 50000; i++) {
 writef("haaaaa ", i, " \r");
 }
 writefln("===================");
 return 0;
 }
 ----------------------------------------------
IIRC writefln starte outputting at the current location and appens a newline at the end. It does not ever add a newline at the start.
The middle one is writef, not writefln. And I think the point of this program is not to put newlines between the outputs, but to keep overwriting the last line (as a progress indicator, I guess). Which is exactly what it does on Linux by the way, with both DMD and GDC.
Sep 27 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Frits,

 BCS wrote:
 
 Reply to dominik,
 
 ----------------------------------------------
 import std.stdio;
 int main(char[][] args)
 {
 writefln("===================");
 for (int i = 0; i < 50000; i++) {
 writef("haaaaa ", i, " \r");
 }
 writefln("===================");
 return 0;
 }
 ----------------------------------------------
IIRC writefln starte outputting at the current location and appens a newline at the end. It does not ever add a newline at the start.
The middle one is writef, not writefln. And I think the point of this program is not to put newlines between the outputs, but to keep overwriting the last line (as a progress indicator, I guess). Which is exactly what it does on Linux by the way, with both DMD and GDC.
that is how I was reading it. The error I think is in question is that the writfln replaces the last line from the writef. I haven't run the code so I could be totaly off base here.
Sep 27 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
BCS wrote:
 Reply to Frits,
 
 BCS wrote:

 Reply to dominik,

 ----------------------------------------------
 import std.stdio;
 int main(char[][] args)
 {
 writefln("===================");
 for (int i = 0; i < 50000; i++) {
 writef("haaaaa ", i, " \r");
 }
 writefln("===================");
 return 0;
 }
 ----------------------------------------------
IIRC writefln starte outputting at the current location and appens a newline at the end. It does not ever add a newline at the start.
The middle one is writef, not writefln. And I think the point of this program is not to put newlines between the outputs, but to keep overwriting the last line (as a progress indicator, I guess). Which is exactly what it does on Linux by the way, with both DMD and GDC.
that is how I was reading it. The error I think is in question is that the writfln replaces the last line from the writef.
Oh wait, I must've misread the original post. Yes, that's what happens (I just didn't realize that was the problem -- I thought he didn't see the counting at all) dominik: Try adding a writefln() right after the loop, or prepending "\n" to the last string.
Sep 27 2007
prev sibling parent reply Gilles G. <schaouette free.fr> writes:
Well, I think on windows \r also clears the current line...
So instead you should use something like:

 import std.stdio;
 
 int main(char[][] args)
 {
 
     writefln("===================");
     for (int i = 0; i < 50000; i++) {
       writef("\rhaaaaa ", i);
     }
     writefln("\n===================");

     return 0;
 }

This should work, but notice that I did not try it...
dominik Wrote:

 ok, I've just unzipped dm and dmd, and I have tried to write my first 
 trivial D program - just to get started (below is the code I wrote). It 
 works, from the first try :) However, it behaves em weird.
 First writefln works like it is supposed to, last one also, however writef 
 should just write in place haaaa and the number (hence the \r). But, when 
 printing in place is done, and the last writefln fires off, line where 
 writef wrote to disappears. How, why?
 
 I'm on windows XP if that matters
 ----------------------------------------------
 import std.stdio;
 
 int main(char[][] args)
 {
 
     writefln("===================");
     for (int i = 0; i < 50000; i++) {
       writef("haaaaa ", i, " \r");
     }
     writefln("===================");
 
     return 0;
 }
 ---------------------------------------------- 
 
 
Sep 27 2007
parent "dominik" <aha aha.com> writes:
"Gilles G." <schaouette free.fr> wrote in message 
news:fdh95q$bir$1 digitalmars.com...
 Well, I think on windows \r also clears the current line...
 So instead you should use something like:
exactly, I just did that and it works. Thanks for help. I am writing tons and tons of "scripts" as command line utilites with PHP both for windows and linux every day, so I was used to the way that works there. Thanks again guys!
Sep 27 2007