Print vs Print #2, results with TAB different

General FreeBASIC programming questions.
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Print vs Print #2, results with TAB different

Post by Flyzone »

I get different results when using the standard output Print vs. printing to a numbered file (i.e. Print #2,) when using TAB. For example, the following statement works placing the second item, a string, in the proper tab position (i.e. 50)

Print a(x,1);tab(50);s

However, the following Print statement seems to ignore the TAB function and prints at the usual default positions:

Print #2,a(x,1);tab(50);s

The documentation does not appear to state there are differences.
fxm
Moderator
Posts: 12551
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Print vs Print #2, results with TAB different

Post by fxm »

For me this works:

Code: Select all

Dim As Integer a(5, 2)
Dim As Integer x = 3
a(x, 1) = 123
Dim As String s = "fxm"

Print a(x,1); Tab(50); s

Open "test.txt" For Output As #2
    Print #2, a(x,1); Tab(50); s
Close #2

Sleep
Output:

Code: Select all

 123                                             fxm
"test.txt":

Code: Select all

 123                                             fxm
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Print vs Print #2, results with TAB different

Post by Flyzone »

Interesting. But by specifying CONS as output, mine doesn't.

Code: Select all

Dim As Integer a(5, 2)
Dim As Integer x = 3
a(x, 1) = 123
Dim As String s = "fxm"

Print a(x,1); Tab(50); s

Open CONS For Output As #2
    Print #2, a(x,1); Tab(50); s
Close #2

Sleep
Output:

Code: Select all

 123                                             fxm
 123          fxm
fxm
Moderator
Posts: 12551
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Print vs Print #2, results with TAB different

Post by fxm »

Flyzone wrote: Apr 19, 2023 0:58 Interesting. But by specifying CONS as output, mine doesn't.

Code: Select all

Dim As Integer a(5, 2)
Dim As Integer x = 3
a(x, 1) = 123
Dim As String s = "fxm"

Print a(x,1); Tab(50); s

Open CONS For Output As #2
    Print #2, a(x,1); Tab(50); s
Close #2

Sleep
Output:

Code: Select all

 123                                             fxm
 123          fxm

Yes, now I see the behavior.

@Jeff,
Using 'Open CONS For Output' to 'Print #' to console:
Tab(x) seems to be always interpreted as a ',' (printing should take place at the next 14 column boundary).
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Print vs Print #2, results with TAB different

Post by Flyzone »

OK. I would be interested to know if/when there might be a resolution, or, if I should just work around it. Thanks for looking, I'm not under any kind of time pressure.
dodicat
Posts: 8242
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Print vs Print #2, results with TAB different

Post by dodicat »

An alternative could be:

Code: Select all

Dim As Integer a(5, 2)
Dim As Integer x = 3
a(x, 1) = 123
Dim As String s = "fxm"

Print a(x,1); Tab(50); s

Open scrn For Output As #2
    Print #2, a(x,1); tab(50); s
Close #2

Sleep 
adeyblue
Posts: 352
Joined: Nov 07, 2019 20:08

Re: Print vs Print #2, results with TAB different

Post by adeyblue »

https://github.com/freebasic/fbc/commit/8b8ec8263efea9a87f65617c3abfb632336a125c wrote: mjs committed on Sep 4, 2005
More TAB() fixes ... TAB also works when using "normal" file I/O but will be translated to PAD when using stdout/stderr (cons, err, pipe)
Open cons, err and pipe all marks the opened file as a pipe, so tab(anynumber) just does what print , does. (PAD is what the , does in Print)

If you redirect the exe output to a file,
Print a(x,1); Tab(50); s
doesn't have any spaces but the Open Cons one does, so perhaps the Tab for cons is on purpose considering its output is consistent.
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Print vs Print #2, results with TAB different

Post by Flyzone »

... perhaps the Tab for cons is on purpose considering its output is consistent.
I doubt it. Consistent to itself? There is nothing in the doc that I can find that would explain that behavior and the inconsistency with other output formats seems illogical (at least to me). I would think a goal would be to avoid device dependent formatting if possible.
dodicat
Posts: 8242
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Print vs Print #2, results with TAB different

Post by dodicat »

I see that open scrn writes to a graphics screen if there is one or the console if there is not.
So it isn't a very good alternative.
Although from the help file:

Description

This command opens the console for both input and output as a file, allowing to read/write from/to it with normal file commands.

So I was a bit confused, and should have tested out open scrn + graphics screen.
In view of that then , until tab gets looked at for open cons, you could use (as a workaround) the crt printf which always goes to the console.

Code: Select all

#include "crt.bi"

screen 12

Dim As Integer a(5, 2)
Dim As Integer x = 3
a(x, 1) = 123
Dim As String s = "fxm"

Printf(!"%d  %50s\n",a(x,1), s)

Print a(x,1); tab(50); s


Sleep 
 
fxm
Moderator
Posts: 12551
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Print vs Print #2, results with TAB different

Post by fxm »

KeyPgOpenScrn → fxm [rewording]
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Print vs Print #2, results with TAB different

Post by Flyzone »

fxm wrote: Apr 20, 2023 11:59 KeyPgOpenScrn → fxm [rewording]
Does this clarify the different functioning of TAB for Print# to CONS?
Last edited by Flyzone on Apr 20, 2023 17:02, edited 2 times in total.
fxm
Moderator
Posts: 12551
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Print vs Print #2, results with TAB different

Post by fxm »

No not yet.
I am waiting for a possible feedback from our developer Jeff before finalizing the update for 'Open ...'.

'Open Scrn' supports 'Tab()', not 'Open Cons/Err/Pipe'
But seen from the user, the problem mainly concerns:
- 'Open Cons',
- 'Open Err'.
dodicat
Posts: 8242
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Print vs Print #2, results with TAB different

Post by dodicat »

Here is another workaround for tab

Code: Select all

#define _tab(n)  space(n-pos)
Dim As Integer a(5, 2)
Dim As Integer x = 3
a(x, 1) = 123
Dim As String s = "fxm"

Print a(x,1); Tab(50); s

Open CONS For Output As #2
    Print #2, a(x,1); _Tab(50); s
Close #2


Open CONS For Output As #2
print "Press any key";_tab(20);"Goodbye"
close #2

Sleep 
 
fxm
Moderator
Posts: 12551
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Print vs Print #2, results with TAB different

Post by fxm »

dodicat wrote: Apr 20, 2023 20:31 Here is another workaround for tab

Not completely:
Tab( col_num )
.....
If the current column is greater than column, then Tab will move the cursor to the requested column number on the next line.
.....
fxm
Moderator
Posts: 12551
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Print vs Print #2, results with TAB different

Post by fxm »

fxm wrote: Apr 20, 2023 16:25 No not yet.
I am waiting for a possible feedback from our developer Jeff before finalizing the update for 'Open ...'.

'Open Scrn' supports 'Tab()', not 'Open Cons/Err/Pipe'
But seen from the user, the problem mainly concerns:
- 'Open Cons',
- 'Open Err'.

At the moment I align the documentation of 'Open Cons/Err' to the current behavior:
- KeyPgOpenCons → fxm [added the output behavior if using TAB]
- KeyPgOpenErr → fxm [added the output behavior if using TAB]
Post Reply