@filewrite oddity (to me)

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
If I have the command in a .btm file

echo %@filewrite[%h,%line]> nul

it does exactly what I want it to do - to write the value of %line into the file opened by handle %h, and I see nothing in my tcc window. However, if %line is empty, it (properly) writes a blank line in my file, but in my tcc window, I see, "ECHO is OFF". Shouldn't the redirection to NUL prevent that?
 
#3
If I have the command in a .btm file

echo %@filewrite[%h,%line]> nul

it does exactly what I want it to do - to write the value of %line into the file opened by handle %h, and I see nothing in my tcc window. However, if %line is empty, it (properly) writes a blank line in my file, but in my tcc window, I see, "ECHO is OFF". Shouldn't the redirection to NUL prevent that?
It works better with a space before ">". Whether that's as it should be, I don't know.
Code:
v:\> set h=%@fileopen[test.txt,w,t]

v:\> echo %@filewrite[%h,%no_exist]> NUL
ECHO is OFF

v:\> echo %@filewrite[%h,%no_exist] > NUL

v:\>
 
#4
It works better with a space before ">". Whether that's as it should be, I don't know.
Code:
v:\> set h=%@fileopen[test.txt,w,t]

v:\> echo %@filewrite[%h,%no_exist]> NUL
ECHO is OFF

v:\> echo %@filewrite[%h,%no_exist] > NUL

v:\>
Vince, you get the gold star. I have no idea why that should matter, but I don't really care. :-) Thank you!
 

rconn

Administrator
Staff member
May 14, 2008
10,210
86
#5
WAD, if a bit unexpected at first.

What's happening is that @FILEWRITE is returning the number of characters written to the file. Since %line is empty, TCC wrote an empty string followed by a CR/LF. So the result of @FILEWRITE is "2", and your command now looks like this:

Code:
echo 2> nul
Now, that syntax does *not* write a "2" to NUL, it redirects STDERR to NUL (see the help). So you wind up with an "echo ", which faithfully reports the status of the ECHO flag.

When you add the space before the >, you get "echo 2 > nul", which redirects STDOUT to NUL, and writes "2" to the bit bucket.

Moral of the story - use the correct syntax (whitespace around redirection characters), and you won't be surprised.