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.
Help with"invalid channel at line..." error - newbie question
-
- Posts: 9
- Joined: Wed 12 Feb 2025, 21:45
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32
Re: Help with"invalid channel at line..." error - newbie question
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
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")
-
- Posts: 9
- Joined: Wed 12 Feb 2025, 21:45
Re: Help with"invalid channel at line..." error - newbie question
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!
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!
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32
Re: Help with"invalid channel at line..." error - newbie question
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

-
- Posts: 9
- Joined: Wed 12 Feb 2025, 21:45
Re: Help with"invalid channel at line..." error - newbie question
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.
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.
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32
Re: Help with"invalid channel at line..." error - newbie question
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.
-
- Posts: 9
- Joined: Wed 12 Feb 2025, 21:45
Re: Help with"invalid channel at line..." error - newbie question
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?
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?
-
- Posts: 9
- Joined: Wed 12 Feb 2025, 21:45
Re: Help with"invalid channel at line..." error - newbie question
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?
-
- Posts: 272
- Joined: Tue 18 Jun 2024, 09:32
Re: Help with"invalid channel at line..." error - newbie question
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).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$)
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$
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.
-
- Posts: 9
- Joined: Wed 12 Feb 2025, 21:45
Re: Help with"invalid channel at line..." error - newbie question
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?
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?