Writing to Files, using OPEN with procedures

I am trying to write to a file but I get always "7004, channel isn't open error" I opened a channel in the main program and then I tried to PRINT to a file inside a procedure. Is there a special format or keyword that I should be using?

I have looked at Chapter 12 of the manual that comes with TrueBasic and it doesn't seem that I am doing anything wrong except that I am printing to a file inside a procedure

Here is the complete program:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

PROGRAM cooler

INPUT PROMPT "WHAT IS THE FILENAME TO WRITE TO? ":filename$
OPEN #2: name filename$ , ORG "TEXT" , CREATE NEWOLD

CALL initial(t, temperature, room_temp, r, dt, ncalc, nprt)
CALL output(t, temperature)
FOR iprt = 1 to nprt
CALL Euler(t, temperature, room_temp, r, dt, ncalc)
CALL output(t, temperature)
NEXT iprt
CLOSE #2
END

SUB initial(t, temperature, room_temp, r, dt, ncalc, nprt)
LET t = 0
LET temperature = 83
LET room_temp = 22
INPUT prompt "cooling constant r = ":r
INPUT prompt "duration = ":tmax
INPUT prompt "time interval = ":dt
INPUT prompt "print period = ":print_period
LET nprt = tmax / print_period
LET ncalc = print_period / dt

PRINT #2:"time", "temperature"
PRINT #2
END SUB

SUB Euler(t, temperature, room_temp, r, dt, ncalc)
FOR icalc = 1 to ncalc
LET change = -r * (temperature - room_temp)
LET temperature = temperature + change * dt
NEXT icalc
LET t = t + dt * ncalc
END SUB

SUB output(t, temperature)
PRINT #2: t, temperature
END SUB

Comments

CHANNEL NUMBERS AS PARAMETERS

Hi TomKC

Mark Harris has given an excellent explanation, but basically this IS covered in the manual; you don't need to "buy" any new books...

Everything TB will do can be found in the free Silver (or Gold) pdf manual on the True Basic downloads page:

True BASIC 6 Silver Edition Manual Download
Silver Edition manual in PDF Format - updated for v6.007.

Quote from: True BASIC 6 Silver Edition Manual - Page 121 of 668:
------------------------
CHANNEL NUMBERS AS PARAMETERS
Subroutines may also use channel numbers as parameters (functions may not). For example:

SUB OpenFile (qu$, #1) ! Open a file
PRINT qu$; ! Prompt user
INPUT f$ ! Name of file
CLOSE #1 ! In case #1 open
OPEN #1: name f$
END SUB

CALL OpenFile(“Data file”, #3) ! Invoke it

The file opened as channel #1 within the subroutine is associated with channel #3 in the calling program. Channel
numbers and their uses are described fully in Chapter 12 “Files for Data Input and Output.”
--------------------------

I hope this helps.
Regards, Mike C.

Thank you...

I believe this is not covered in the manual. Looks like I will be buying more books on TrueBasic. Thanks so much, again!

subroutine and scope

This is a restriction of scope; namely, your subroutine (sub - end sub) only 'knows' about the variables ( or channels ) that you have passed it as parameters. It does not 'know' that the channel is OPEN, hence the 7004 error.

You may pass the open channel number to your subroutine as a parameter (maybe the last one) if you like. I have simplified the issue in the following code; it is a very simple mainline, and another very simple sub out_put(). I am passing my open channel number (#1) as a parm to out_put(), and then within out_put() I refer to that channel as #7.


!
!
open #1: name "myfile.txt", create "newold", org "text"
call out_put("Now is the time for all good men", #1)
call out_put("to come to the aid of their country.", #1)
close #1
end

sub out_put(aa$, #7)
reset #7: end
print #7: aa$
end sub

OK, please note that the subroutine has been placed AFTER the program mainline end. This makes the subroutine external to the mainline so that it has its own scope. Now, take a look at the following code; I have placed the subroutine at the top of the mainline definitely ahead of the end within the same program unit (same as mainline).


!
!

sub out_put(aa$)
reset #1: end
print #1: aa$
end sub

open #1: name "mytext.txt", create "newold", org "text"

call out_put("now is the time for all good men")
call out_put("to come t the aid of their country")
close #1
end

In the code above local variables ( and open channels ) will be available to the subroutine because it has been placed within the scope of the mainline (ahead of the end), because the sub is within the same program unit as the mainline.

I don't really 'like' global variables, so I almost always place my subroutines outside the mainline, and usually in another file (library) completely. This decision is of course up to you.

marcus

PS Scope and Aliasing are fully discussed in the True BASIC Reference Manual (1988) in appendix E. pp415-ff

PSS Other reference works like "Let's Program it in True Basic," or "A World Tour of BASIC ..." discuss scope in the subroutine sections specifically the differences between internal and external subroutines. The EXTERNAL keyword is optional (the compiler determines if the sub is external) but the rule(s) are very simple; variables and channels are 'local' to the program unit, and are EXTERNAL outside the program unit.

-----------------------------------------------

#2 error

Do not forget the statement:

reset #2: end

Text files do not allow to be witten in the middle.

Other way to solve the problema instead of passin #2 throug the cal sequence is to change the position of the "END" line to the end of the code file.

#2 error

Do not forget the statement:

reset #2: end

Text files do not allow to be written in the middle.

Besides that, other way to solve other problem perspective: instead of passin #2 throug the cal sequence is to change the position of the "END" line to the end of the code file.

halo books truebasic

halo

An Introduction to Computer Simulation Methods: Applications to Physical System '2nd Edition'

code : truebasic . ( Harvey Gould)

Mathematical Models of Continuous and Discrete Dynamic Systems (Ellis Horwood Series in Mathematics & Its Applications)
H. B. Griffiths

code : truebasic

my website:

https://petervlaschemeng.weebly.com/