www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I seem to be able to crash writefln

reply Joel Christensen <joelcnz gmail.com> writes:
This is on Windows 7. Using a def file to stop the terminal window 
coming up.

win.def
EXETYPE NT
SUBSYSTEM WINDOWS

bug.d
import std.stdio;
import std.string;

void main() {
	auto f = File( "z.txt", "w" );
	scope( exit )
		f.close;
	string foo = "bar";
	foreach( n; 0 .. 10 ) {
		writefln( "%s", foo );
		f.write( format( "count duck-u-lar: %s\n", n ) );
	}
}

output (from in z.txt):
count duck-u-lar: 0
Mar 09 2011
next sibling parent reply spir <denis.spir gmail.com> writes:
On 03/10/2011 12:19 AM, Joel Christensen wrote:
 This is on Windows 7. Using a def file to stop the terminal window coming up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 auto f = File( "z.txt", "w" );
 scope( exit )
 f.close;
 string foo = "bar";
 foreach( n; 0 .. 10 ) {
 writefln( "%s", foo );
 f.write( format( "count duck-u-lar: %s\n", n ) );
 }
 }

 output (from in z.txt):
 count duck-u-lar: 0
What do you mean, crashing writefln? What do you get on the terminal? About the file, there seems to be a bug --but unrelated to writefln. The file is closed, I guess because of scope(exit), before the output stream is flushed. If this is the right interpretation, then there is a precedence issue; scope's action should not be performed before the func's own action is actually completed. Denis -- _________________ vita es estrany spir.wikidot.com
Mar 09 2011
parent Joel Christensen <joelcnz gmail.com> writes:
On 10-Mar-11 1:04 PM, spir wrote:
 On 03/10/2011 12:19 AM, Joel Christensen wrote:
 This is on Windows 7. Using a def file to stop the terminal window
 coming up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 auto f = File( "z.txt", "w" );
 scope( exit )
 f.close;
 string foo = "bar";
 foreach( n; 0 .. 10 ) {
 writefln( "%s", foo );
 f.write( format( "count duck-u-lar: %s\n", n ) );
 }
 }

 output (from in z.txt):
 count duck-u-lar: 0
What do you mean, crashing writefln? What do you get on the terminal? About the file, there seems to be a bug --but unrelated to writefln. The file is closed, I guess because of scope(exit), before the output stream is flushed. If this is the right interpretation, then there is a precedence issue; scope's action should not be performed before the func's own action is actually completed. Denis
It quits out the at about the 2nd attempt at printing text (that doesn't go to the terminal because of the def file argument in the compile arguments). I didn't see any problem with the File stuff, I can use writeln and it does all ten iterations, (not printing any thing of course). I used write instead of writeln, write doesn't flush like writeln, maybe. I noticed my program that had been running fine before, but suddenly bailed out almost strait away since I used the def file in the compile arguments. Joel
Mar 09 2011
prev sibling next sibling parent reply Andrew Wiley <debio264 gmail.com> writes:
On Wed, Mar 9, 2011 at 5:19 PM, Joel Christensen <joelcnz gmail.com> wrote:
 This is on Windows 7. Using a def file to stop the terminal window coming
 up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 =A0 =A0 =A0 =A0auto f =3D File( "z.txt", "w" );
 =A0 =A0 =A0 =A0scope( exit )
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.close;
 =A0 =A0 =A0 =A0string foo =3D "bar";
 =A0 =A0 =A0 =A0foreach( n; 0 .. 10 ) {
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0writefln( "%s", foo );
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.write( format( "count duck-u-lar: %s\n",=
n ) );
 =A0 =A0 =A0 =A0}
 }

 output (from in z.txt):
 count duck-u-lar: 0
My understanding is that the "0..10" isn't actually a range notation, and you need to use iota(0, 10). I may be wrong, but if I'm right, hopefully someone can explain why this syntax works? I remember there being a discussion about this recently; I'll see if I can find it.
Mar 09 2011
parent reply Ary Manzana <ary esperanto.org.ar> writes:
On 3/10/11 4:15 AM, Andrew Wiley wrote:
 On Wed, Mar 9, 2011 at 5:19 PM, Joel Christensen<joelcnz gmail.com>  wrote:
 This is on Windows 7. Using a def file to stop the terminal window coming
 up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
         auto f = File( "z.txt", "w" );
         scope( exit )
                 f.close;
         string foo = "bar";
         foreach( n; 0 .. 10 ) {
                 writefln( "%s", foo );
                 f.write( format( "count duck-u-lar: %s\n", n ) );
         }
 }

 output (from in z.txt):
 count duck-u-lar: 0
My understanding is that the "0..10" isn't actually a range notation, and you need to use iota(0, 10). I may be wrong, but if I'm right, hopefully someone can explain why this syntax works? I remember there being a discussion about this recently; I'll see if I can find it.
It works because it's a specialized syntax for foreach. Oh, and I think in case statements you can use it too, but I don't remember if the first was inclusive and the second exclusive, or both, or what. But... it just works for those cases. For the rest you have to use iota...
Mar 10 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Ary Manzana:

 It works because it's a specialized syntax for foreach. Oh, and I think 
 in case statements you can use it too, but I don't remember if the first 
 was inclusive and the second exclusive, or both, or what. But... it just 
 works for those cases. For the rest you have to use iota...
Yes, it's a quite messy design. And Walter & Andrei seem to think it's OK. Bye, bearophile
Mar 10 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday 09 March 2011 23:15:13 Andrew Wiley wrote:
 On Wed, Mar 9, 2011 at 5:19 PM, Joel Christensen <joelcnz gmail.com> wrote:
 This is on Windows 7. Using a def file to stop the terminal window coming
 up.
 
 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS
 
 bug.d
 import std.stdio;
 import std.string;
 
 void main() {
        auto f = File( "z.txt", "w" );
        scope( exit )
                f.close;
        string foo = "bar";
        foreach( n; 0 .. 10 ) {
                writefln( "%s", foo );
                f.write( format( "count duck-u-lar: %s\n", n ) );
        }
 }
 
 output (from in z.txt):
 count duck-u-lar: 0
My understanding is that the "0..10" isn't actually a range notation, and you need to use iota(0, 10). I may be wrong, but if I'm right, hopefully someone can explain why this syntax works? I remember there being a discussion about this recently; I'll see if I can find it.
0..10 works with foreach. It's specific to foreach. iota also works, because it produces a range rather being built in to the language. As such, iota works in places _other_ than foreach. But 0..10 works just fine in foreach. It definitely pre-dates iota. - Jonathan M Davis
Mar 09 2011
prev sibling next sibling parent reply Andrew Wiley <debio264 gmail.com> writes:
On Thu, Mar 10, 2011 at 1:31 AM, Jonathan M Davis <jmdavisProg gmx.com> wro=
te:
 On Wednesday 09 March 2011 23:15:13 Andrew Wiley wrote:
 On Wed, Mar 9, 2011 at 5:19 PM, Joel Christensen <joelcnz gmail.com> wro=
te:
 This is on Windows 7. Using a def file to stop the terminal window com=
ing
 up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 =A0 =A0 =A0 =A0auto f =3D File( "z.txt", "w" );
 =A0 =A0 =A0 =A0scope( exit )
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.close;
 =A0 =A0 =A0 =A0string foo =3D "bar";
 =A0 =A0 =A0 =A0foreach( n; 0 .. 10 ) {
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0writefln( "%s", foo );
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.write( format( "count duck-u-lar: %s\=
n", n ) );
 =A0 =A0 =A0 =A0}
 }

 output (from in z.txt):
 count duck-u-lar: 0
My understanding is that the "0..10" isn't actually a range notation, and you need to use iota(0, 10). I may be wrong, but if I'm right, hopefully someone can explain why this syntax works? I remember there being a discussion about this recently; I'll see if I can find it.
0..10 works with foreach. It's specific to foreach. iota also works, beca=
use it
 produces a range rather being built in to the language. As such, iota wor=
ks in
 places _other_ than foreach. But 0..10 works just fine in foreach. It def=
initely
 pre-dates iota.

 - Jonathan M Davis
Ah, then I guess I just need to learn me some D. Sorry for the noise.
Mar 09 2011
parent Ary Manzana <ary esperanto.org.ar> writes:
On 3/10/11 4:40 AM, Andrew Wiley wrote:
 On Thu, Mar 10, 2011 at 1:31 AM, Jonathan M Davis<jmdavisProg gmx.com>  wrote:
 On Wednesday 09 March 2011 23:15:13 Andrew Wiley wrote:
 On Wed, Mar 9, 2011 at 5:19 PM, Joel Christensen<joelcnz gmail.com>  wrote:
 This is on Windows 7. Using a def file to stop the terminal window coming
 up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
         auto f = File( "z.txt", "w" );
         scope( exit )
                 f.close;
         string foo = "bar";
         foreach( n; 0 .. 10 ) {
                 writefln( "%s", foo );
                 f.write( format( "count duck-u-lar: %s\n", n ) );
         }
 }

 output (from in z.txt):
 count duck-u-lar: 0
My understanding is that the "0..10" isn't actually a range notation, and you need to use iota(0, 10). I may be wrong, but if I'm right, hopefully someone can explain why this syntax works? I remember there being a discussion about this recently; I'll see if I can find it.
0..10 works with foreach. It's specific to foreach. iota also works, because it produces a range rather being built in to the language. As such, iota works in places _other_ than foreach. But 0..10 works just fine in foreach. It definitely pre-dates iota. - Jonathan M Davis
Ah, then I guess I just need to learn me some D. Sorry for the noise.
No, D needs to change, not you. Ah, consistency...
Mar 10 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 09 Mar 2011 18:19:55 -0500, Joel Christensen <joelcnz gmail.com>  
wrote:

 This is on Windows 7. Using a def file to stop the terminal window  
 coming up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 	auto f = File( "z.txt", "w" );
 	scope( exit )
 		f.close;
 	string foo = "bar";
 	foreach( n; 0 .. 10 ) {
 		writefln( "%s", foo );
 		f.write( format( "count duck-u-lar: %s\n", n ) );
 	}
 }

 output (from in z.txt):
 count duck-u-lar: 0
If I dust off my rusty old Windows hat, I believe if you try to write to stdout while there is no console window, you will encounter an error. So don't do that ;) I'm not sure what you were expecting... -Steve
Mar 10 2011
parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
On 10/03/2011 12:18, Steven Schveighoffer wrote:
 On Wed, 09 Mar 2011 18:19:55 -0500, Joel Christensen <joelcnz gmail.com>
 wrote:

 This is on Windows 7. Using a def file to stop the terminal window
 coming up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 auto f = File( "z.txt", "w" );
 scope( exit )
 f.close;
 string foo = "bar";
 foreach( n; 0 .. 10 ) {
 writefln( "%s", foo );
 f.write( format( "count duck-u-lar: %s\n", n ) );
 }
 }

 output (from in z.txt):
 count duck-u-lar: 0
If I dust off my rusty old Windows hat, I believe if you try to write to stdout while there is no console window, you will encounter an error. So don't do that ;) I'm not sure what you were expecting... -Steve
You normally get no output AFAIR using c++ type compiled programs with printf or cout -- and perhaps an error set in cout, but I can't remember about that now.
Mar 11 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 11 Mar 2011 18:57:32 -0500, Spacen Jasset  
<spacenjasset yahoo.co.uk> wrote:

 On 10/03/2011 12:18, Steven Schveighoffer wrote:
 On Wed, 09 Mar 2011 18:19:55 -0500, Joel Christensen <joelcnz gmail.com>
 wrote:

 This is on Windows 7. Using a def file to stop the terminal window
 coming up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 auto f = File( "z.txt", "w" );
 scope( exit )
 f.close;
 string foo = "bar";
 foreach( n; 0 .. 10 ) {
 writefln( "%s", foo );
 f.write( format( "count duck-u-lar: %s\n", n ) );
 }
 }

 output (from in z.txt):
 count duck-u-lar: 0
If I dust off my rusty old Windows hat, I believe if you try to write to stdout while there is no console window, you will encounter an error. So don't do that ;) I'm not sure what you were expecting... -Steve
You normally get no output AFAIR using c++ type compiled programs with printf or cout -- and perhaps an error set in cout, but I can't remember about that now.
But don't forget, D uses DMC's runtime, not Visual Studio. Make sure you are comparing to DMC compiled programs. -Steve
Mar 14 2011
parent Joel Christensen <joelcnz gmail.com> writes:
On 15/03/2011 1:57 a.m., Steven Schveighoffer wrote:
 On Fri, 11 Mar 2011 18:57:32 -0500, Spacen Jasset
 <spacenjasset yahoo.co.uk> wrote:

 On 10/03/2011 12:18, Steven Schveighoffer wrote:
 On Wed, 09 Mar 2011 18:19:55 -0500, Joel Christensen <joelcnz gmail.com>
 wrote:

 This is on Windows 7. Using a def file to stop the terminal window
 coming up.

 win.def
 EXETYPE NT
 SUBSYSTEM WINDOWS

 bug.d
 import std.stdio;
 import std.string;

 void main() {
 auto f = File( "z.txt", "w" );
 scope( exit )
 f.close;
 string foo = "bar";
 foreach( n; 0 .. 10 ) {
 writefln( "%s", foo );
 f.write( format( "count duck-u-lar: %s\n", n ) );
 }
 }

 output (from in z.txt):
 count duck-u-lar: 0
If I dust off my rusty old Windows hat, I believe if you try to write to stdout while there is no console window, you will encounter an error. So don't do that ;) I'm not sure what you were expecting... -Steve
You normally get no output AFAIR using c++ type compiled programs with printf or cout -- and perhaps an error set in cout, but I can't remember about that now.
But don't forget, D uses DMC's runtime, not Visual Studio. Make sure you are comparing to DMC compiled programs. -Steve
writeln works, just not writefln so much. Haven't tried eg. writeln( format( "%s", value ) ); Can have a version system for with, and with out the terminal. Just don't think I need to do more than just adding and removing the .def file from the compiler arguments. -Joel
Mar 15 2011