Help with"invalid channel at line..." error - newbie question

Discussions related to database technologies, file handling, directories and storage
JMR
Posts: 9
Joined: Wed 12 Feb 2025, 21:45

Help with"invalid channel at line..." error - newbie question

Post by JMR »

I've written a program that determines the number of carbon atoms(nc), hydrogen atoms(nh), oxygen atoms(no) and nitrogen atoms(no) that are present in every molecule that can exist for a given relative molecular mass. However, in trying to create a data file and PRINT# to it I keep getting an "invalid channel at line 645" message. The section of code is:

630 PRINT nc;" ";nh;" ";no;" ";nn
640 chonresults=OPENOUT "CHONRESULTS.DAT"
645 PRINT#nc,nh,no,nn
646 CLOSE#chonresults
650 UNTIL nc*12 >= R
660
670 UNTIL no*16 >= R
680
690 UNTIL nn*14 >= R
Why am I getting this error please and how do I rectify it? Thanking you in anticipation.
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Help with"invalid channel at line..." error - newbie question

Post by Richard Russell »

JMR wrote: Sun 09 Mar 2025, 20:23 Why am I getting this error please and how do I rectify it? Thanking you in anticipation.
Add this line, or similar, to your program and run it again:

Code: Select all

 642 IF chonresults = 0 PRINT "Couldn't create CHONRESULTS.DAT" : STOP
My guess is that you will find that the file could not be created. You will then need to diagnose why, a common cause is that the directory in which you are trying to create it is not writable by a regular user but only by an administrator.

One way of avoiding this problem is to store the file in the @usr$ directory, because that is guaranteed to be writable:

Code: Select all

  640 chonresults=OPENOUT(@usr$ + "CHONRESULTS.DAT")
If that's not the cause, post here again.
JMR
Posts: 9
Joined: Wed 12 Feb 2025, 21:45

Re: Help with"invalid channel at line..." error - newbie question

Post by JMR »

It's still giving the same error code and doesn't print "Couldn't create CHONRESULTS.DAT". How do find out the channel? It does produce the .dat file after I added the @usr bit but with zero data.
Could it be that I'm repeatedly trying to write to a file whose channel I don't know? I'm probably clutching at straws and showing my ignorance!
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Help with"invalid channel at line..." error - newbie question

Post by Richard Russell »

JMR wrote: Sun 09 Mar 2025, 21:31 It's still giving the same error code and doesn't print "Couldn't create CHONRESULTS.DAT". How do find out the channel? It does produce the .dat file after I added the @usr bit but with zero data.
Hmm. I assume that line 645 is not really what you listed, because you omitted the channel number entirely:

Code: Select all

645 PRINT#chonresults,nc,nh,no,nn
I didn't comment on that previously because I assumed it was a typo, but if it's what's you've actually got in the program that would explain it. :roll:
JMR
Posts: 9
Joined: Wed 12 Feb 2025, 21:45

Re: Help with"invalid channel at line..." error - newbie question

Post by JMR »

I'm terribly sorry...now I know how it works....thank you very much.
I now have a new problem. How do I open the file? Notepad and Wordpad produce unusual symbols and BBC Basic for Windows prduces the word "circle" in orange.
I'm lost. I hope that I'm not wasting your time. The actual program works like a dream - it works for any R.M.M. - it's just that it can produce so many valid answers for a particular, large R.M.M. that I would like to write them to a data file as well as the screen.
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Help with"invalid channel at line..." error - newbie question

Post by Richard Russell »

JMR wrote: Sun 09 Mar 2025, 22:48 I now have a new problem. How do I open the file? Notepad and Wordpad produce unusual symbols...
You open it using BBC BASIC! PRINT# and INPUT# are intended to be used together: a data file is written with PRINT# and read with INPUT#, the data in the file is in a special internal 'binary' format, in order to make this fast and accurate, and the file typically won't make sense to any other application (even other versions of BBC BASIC).

It sounds as though you are trying to generate a 'compatible' file, that is one written by BBC BASIC but read by a different application; a 'plain text' file would be in that category. There's an entire Wiki article on that subject, although it's arguably somewhat out-of-date because it doesn't mention BPUT#file,string$ and GET$#file which were added to BBC BASIC (in 1986!) specifically for this purpose.
JMR
Posts: 9
Joined: Wed 12 Feb 2025, 21:45

Re: Help with"invalid channel at line..." error - newbie question

Post by JMR »

A 'plain text' file is exactly what I want to produce.

I'm now getting a new error: 'no such variable at line 648' with the following addition to my program:
LF = 10
648 outfile% = OPENOUT(@usr$ + CHONRESULTS$)

REM PRINT #outfile%, text$
REM BPUT #outfile%, LF

PRINT #outfile%, STR$(nc),STR$(nh),STR$(no),STR$(nn)
BPUT #outfile%, LF

REM etc. for each line in the file

CLOSE #outfile%
650 UNTIL nc*12 >= R
660
670 UNTIL no*16 >= R
680
690 UNTIL nn*14 >= R

what have I done wrong?
JMR
Posts: 9
Joined: Wed 12 Feb 2025, 21:45

Re: Help with"invalid channel at line..." error - newbie question

Post by JMR »

I've now partially solved it by replacing (@usr$ + CHONRESULTS$) with (@usr$ + "CHONRESULTS.txt") to produce a text file but the text file only contains the last result. Furthermore, this result is written as a column of 4 numbers rather than a row. How do I solve this please?
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Help with"invalid channel at line..." error - newbie question

Post by Richard Russell »

JMR wrote: Mon 10 Mar 2025, 15:34 I'm now getting a new error: 'no such variable at line 648' with the following addition to my program:

Code: Select all

  648     outfile% = OPENOUT(@usr$ + CHONRESULTS$)
Check the line in which CHONRESULTS$ is defined, I'm wondering if there's a typo (maybe confusing CHONRESULTS$ with CHONRESULT$ but that's just a guess).

Try to hone your debugging skills, because your progress will be frustratingly slow if you keep having to ask for help. Take full advantage of the facilities provided, for example if you get a 'No such variable' error and you're not sure which variable it was, use an immediate-mode command to identify it:

Code: Select all

PRINT CHONRESULTS$
Once you've identified which variable is undefined you can either use Find in the IDE to search your program for all occurrences of it, or double-click on one occurrence which causes the others to be highlighted in light blue.

It may also be helpful to run your program in the debugger, then all the defined variables are shown in the List variables window and you can see both their names and values there.
JMR
Posts: 9
Joined: Wed 12 Feb 2025, 21:45

Re: Help with"invalid channel at line..." error - newbie question

Post by JMR »

I've now replaced what I'd had with the following:
630 PRINT nc;" ";nh;" ";no;" ";nn
LF = 10
CHONRESULTS$ = "CHONRESULTS.txt"
outfile% = OPENUP(@usr$ + CHONRESULTS$)
PRINT#outfile%,STR$(nc),STR$(nh),STR$(no),STR$(nn)
BPUT#outfile%,LF
REM etc. for each line in the file
CLOSE #outfile%
650 UNTIL nc*12 >= R
660
670 UNTIL no*16 >= R
680
690 UNTIL nn*14 >= R
This now leads to a CHONRESULTS.txt file at last and no error messages. However, if the output to the screen is, for example:
6 4 0 0
3 8 2 0
2 4 3 0
2 8 1 2
1 4 2 2
1 8 0 4
the output to CHONRESULTS.txt is:
1
8
0
4
i.e. all previous results are over-written by the last result and it's written verically rather than horizontally.
All I require is to write all results that appear on the screen, to a text file ...it can't be that difficult. What am I doing wrong please? Also, what does the " REM etc. for each line in the file" bit mean?