BBC BASIC for Windows
« Moving and array between programs »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 9:54pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Moving and array between programs  (Read 63 times)
chrispc
New Member
Image


member is offline

Avatar




PM


Posts: 19
xx Moving and array between programs
« Thread started on: Oct 9th, 2014, 12:22pm »

I have an array in a finished BBC4W program and I want to use it and its information in another program. Is there a way I can transfer it?
User IP Logged

rtr2
Guest
xx Re: Moving and array between programs
« Reply #1 on: Oct 9th, 2014, 12:44pm »

on Oct 9th, 2014, 12:22pm, chrispc wrote:
I have an array in a finished BBC4W program and I want to use it and its information in another program. Is there a way I can transfer it?

Write it to a file, and get the other program to read the file. Or am I missing the point?

Coding FOR...NEXT loops to read and write the array is simple enough, but there are 'universal' routines (which automatically adapt to the type and size of the array) here:

http://bb4w.wikispaces.com/Reading+and+writing+arrays+in+files

(although they may need to be modified to work correctly in FLOAT 64 mode or with BB4W version 6).

Richard.
User IP Logged

chrispc
New Member
Image


member is offline

Avatar




PM


Posts: 19
xx Re: Moving and array between programs
« Reply #2 on: Oct 24th, 2014, 07:05am »

I have followed your advice and read those pages but am still flummoxed. I hope you can look at my code and tell me what is wrong. The first program (n.10) creates and then tries to save a file; the second(n.11) tries to recover that file. I have used an array of only one dimension to simplify these first efforts, but if I can get that right then I will want to do the same thing with a 3-dim array, so maybe you could add comment about that situation too.
10
20 REM: THIS IS PROGRAM "TEST FILE HANDLING10".
30 REM: I AM TESTING FILE HANDLING WITH THIS ONE-DIMENSIONAL FILE
40 REM: BEFORE TRYING TO USE IT WITH A 3-DIM FILE IN MY MAIN PROGRAM.
50
60 REM: I HAVE TRIED VARIOUS COMBINATIONS OF OPENOUT, OPENUP,
70 REM: OPENIN etc. and still cannot get it to work. Help please.
80 REM: Also see my program "TEST FILE HANDLING11" in which I try ro retrieve this file.
90 REM----------------------------------------------------
100
110 DIM GIN(12)
120
130 X=0
140 A=0
150 REPEAT
160 X=X+1
170 A=A+3
180 GIN(X)= A
190 PRINT"GIN(";X;") = ";GIN(X)
200 G$=GET$
210 UNTIL X=10
220 PRINT"GIN(X) PRINTING FINISHED"
230 REM: GIN(X) stands in for the already-created file N(X,Y,Z) in the program SUDOKU80.
240 REM---------------------------------------------------------------------------------
250 REM: NOW I WILL TRY TO SAVE THE FILE "GIN", AS "GinFile".
260 PRINT""
270 GinFileNum%= OPENOUT ("GinFile.dat")
280 REM: "GinFile" is the name I have given to my newly created file.
290 REM: GinFileNum% is my name for the number the computer has assigned to that new file.
300 PRINT"GinFileNum%= ";GinFileNum%
310 REM: This returns the channel number 5.
320 PRINT"AFTER OPENOUT GinFile"
330 M=0
340 L=OPENIN"GinFileNum"
350 REPEAT
360 M=M+1
370
380 PRINT#GIN(M)
390 PRINT"M= ";M
400 UNTIL M=10
410 PRINT"PRINTING FINISHED"
420 END
=================================
10 REM: THIS IS TEST FILE HANDLING11.
20 REM: I AM TRYING TO RETRIEVE THE FILE I TRIED TO SAVE IN PROGRAM "TEST FILE HANDLING10".
30 REM: THIS DOESN'T WORK. IT GIVES ALL Found(F) =0. What am I doing wrong?
40 DIM Found(20)
50 fnum = OPENIN("GinFile")
60 PRINT"fnum = ";fnum
70 IF fnum=0 THEN PRINT "No GinFile data.":END
80
90 F=0
100 REPEAT
110 F=F+1
120 INPUT#fnum,Found(F)
130
140 PRINT "Found(";F;")= "; Found(F)
150 REM: THESE SHOULD BE MULTIPLES OF 3, BUT ALL=0. WHY??
160
170 UNTIL F=12
180 CLOSE#fnum
190 PRINT"INPUT FINISHED"
200 END
==================================
Thanks for your time.
chrispc

User IP Logged

rtr2
Guest
xx Re: Moving and array between programs
« Reply #3 on: Oct 24th, 2014, 09:14am »

on Oct 24th, 2014, 07:05am, chrispc wrote:
I hope you can look at my code and tell me what is wrong.

The main problem would appear to be this line, which isn't valid syntax and does nothing at all (arguably it should report an error message, but because the 'print list' is empty it doesn't):

Code:
380 PRINT#GIN(M) 

Richard.
User IP Logged

Edja
Developer

member is offline

Avatar




PM


Posts: 60
xx Re: Moving and array between programs
« Reply #4 on: Oct 24th, 2014, 5:16pm »

Code:
300 PRINT"GinFileNum%= ";GinFileNum%
310 REM: This returns the channel number 5 
Overhere GinFileNum% equals 0, meaning the file has not been created.
Also, you didn't write any data to the file.
After creating the file and writing to it (which you didn't), I would close it before opening the file again with OPENIN. This is just a cosmetic measure and not strictly necessary.

Code:
340 L=OPENIN"GinFileNum" 
Shouldn't this be Code:
340 L=OPENIN "GinFile.dat" 

Code:
380 PRINT#GIN(M) 
Remove the #.
Before you can hope to print the values in GIN() you first need to add a read statement retrieving the values that you tried to write to file in the first part (but didn't). Use BGET#,GET#, INPUT# or READ#.

There may be more issues but this should already help you a bit further. Have fun !
Eddy
« Last Edit: Oct 24th, 2014, 9:07pm by Edja » User IP Logged

rtr2
Guest
xx Re: Moving and array between programs
« Reply #5 on: Oct 25th, 2014, 5:30pm »

on Oct 24th, 2014, 5:16pm, Edja wrote:
Code:
   380 PRINT#GIN(M) 
Remove the #.

No, that's entirely wrong. The OP's mistake was not to include the # but to omit the file channel number! The correct version of the line is:

Code:
   380 PRINT #GinFileNum%,GIN(M) 

As I explained in my earlier reply, you could argue that BB4W should have reported an error message as a result of specifying an invalid channel number, but because the 'print list' was empty it was happy to output nothing to an invalid channel!

Richard.
« Last Edit: Oct 25th, 2014, 5:33pm by rtr2 » User IP Logged

Edja
Developer

member is offline

Avatar




PM


Posts: 60
xx Re: Moving and array between programs
« Reply #6 on: Oct 26th, 2014, 11:28am »

Code:
The OP's mistake was not to include the # but to omit the file channel number! 
Yes, that was another possible view. I looked at it differently. Let's have a look at the code. Code:
  330 M=0
  340 L=OPENIN "GinFileNum"
  350 REPEAT
  360   M=M+1
  380   INPUT#L,GIN(M)
  390   PRINT"M= ";M
  400 UNTIL M=10 
Because the OP tries to open the file a second time (line 340) with OPENIN (=for reading, not writing, purposes) I expected a line in the REPEAT...UNTIL loop to read the file's content, something like INPUT#L,GIN(M). Why else is there the OPENIN in line 340? This means to me that one creates a file handle to read from a file that was previously written to. The purpose of line 380 was then to show the data retrieved from the file on screen with a PRINT statement. In that case the # should have been removed. And line 340 should be L=OPENIN ("GinFile.dat")
So, for clarity, I thought the intention was to code something like : Code:
  330 M=0
  340 L=OPENIN("GinFile.dat")
  350 REPEAT
  360   M=M+1
  370   INPUT#L,GIN(M)
  380   PRINT GIN(M)
  390   PRINT"M= ";M
  400 UNTIL M=10
  405 CLOSE#L
 
It boils down to how one interpretes what the OP is trying to accomplish. But, admittedly, looking at the entire program, I think your interpretation is closer to the OP's intentions. May he benefit from this discussion and see where the code can be improved.

I also must admit that I still haven't figured out why GinFileNum%=0 after execution

Eddy
User IP Logged

rtr2
Guest
xx Re: Moving and array between programs
« Reply #7 on: Oct 26th, 2014, 5:05pm »

on Oct 26th, 2014, 11:28am, Edja wrote:
Why else is there the OPENIN in line 340?

My assumption is that Line 340 is spurious, probably left over from some earlier work. It achieves nothing, but it does little harm either.

Quote:
I looked at it differently. Let's have a look at the code. Code:
  330 M=0
  340 L=OPENIN "GinFileNum"
  350 REPEAT
  360   M=M+1
  380   INPUT#L,GIN(M)
  390   PRINT"M= ";M
  400 UNTIL M=10 

That isn't the OP's code - the line 380 you show doesn't exist anywhere in the OP's listing!

Quote:
It boils down to how one interpretes what the OP is trying to accomplish.

The OP explained what he is trying to accomplish in his initial post: he wishes to transfer the contents of an array from one program to another. So he lists one program to write an array to a file, followed by another program to read a file into an array. Any other interpretation seems perverse to me.

Quote:
I also must admit that I still haven't figured out why GinFileNum%=0 after execution

It isn't, GinFileNum% is 5. It would be zero if the Current Directory was set to an unwritable place, of course (such as it might be if you ran the OP's program without saving it first).

Richard.
User IP Logged

Edja
Developer

member is offline

Avatar




PM


Posts: 60
xx Re: Moving and array between programs
« Reply #8 on: Oct 26th, 2014, 11:39pm »

Quote:
line 380 you show doesn't exist anywhere in the OP's listing!
Very true. I've messed up.
Quote:
My assumption is that Line 340 is spurious, probably left over from some earlier work. It achieves nothing, but it does little harm either.
But it did . It doesn't change the result of the program but in the intrest of debugging it shouldn't have been there. Because of that line, and compounded with the error in line 380, I mistakenly reasoned that the OP (and, yes, in spite of the REM statements) tried to open the file to be read from. But following YOUR reasoning I fully agree that the lines following where a (flawed) attempt to write GIN() to the file and not to the screen.
Quote:
Any other interpretation seems perverse to me
Let's not mince words. smiley
Quote:
It would be zero if the Current Directory was set to an unwritable place, of course (such as it might be if you ran the OP's program without saving it first)
I 've saved it his time and ... you're right !! That too threw me off. But the error is all mine.

Lessons learned !

Eddy
User IP Logged

Edja
Developer

member is offline

Avatar




PM


Posts: 60
xx Re: Moving and array between programs
« Reply #9 on: Oct 26th, 2014, 11:47pm »

Code:
30 REM: THIS DOESN'T WORK. IT GIVES ALL Found(F) =0. What am I doing wrong?
40 DIM Found(20)
50 fnum = OPENIN("GinFile")
 

If you replace line 50 by
Code:
50 fnum = OPENIN("GinFile.dat")
 
then it should work.

Eddy
User IP Logged

chrispc
New Member
Image


member is offline

Avatar




PM


Posts: 19
xx Re: Moving and array between programs
« Reply #10 on: Oct 27th, 2014, 10:37pm »

Thanks for your suggestions. I now respond as follows:

1. You say about statement 300: “Overhere GinFileNum% equals 0…”
What does “Overhere” mean? Does it mean on your system when you run the program?
Does that mean that when you run my program you don’t get the same result for the variable GinFileNum% that I get when I run the program? As my REMARK 310 said, I get 5; do you really get 0? If so, doesn’t that mean there is something seriously wrong with either your setup or with mine?

2. I am grateful for all the attention you have both paid to my problem, but I am now very confused, largely because I don’t know enough to understand all of your discussion. I have made some changes to my programs following (some of?) your suggestions, and have reached the following programs: Test File Handling10 has become Test File Handling12, and Test File Handling11 has become Test File Handling13. Can we please start again from those versions i.e. 11 and 13, which I copy below.

3. The last change I made, i.e. changing line 50 from

fnum=OPENIN(“GinFile”)

to

fnum=OPENIN(“GinFile.dat”)

made no difference to my result; I still got all Found(F)=0, as I explain in my statements 10 and 15.

I am sorry to be such a nuisance, but am comforted by seeing the experts finding it tricky too.

Thanks
chrispc
---------------------------------------------------------------
10 REM: THIS IS PROGRAM "TEST FILE HANDLING12".
30 REM: IT IS N.10 BUT WITH THE CHANGES [[unfinished !!]] SUGGESTED ONLINE BY RICHARD(g4bau) and Edja.
35 REM: I AM TESTING FILE HANDLING WITH THIS ONE-DIMENSIONAL FILE
40 REM: BEFORE TRYING TO USE IT WITH A 3-DIM FILE IN MY MAIN PROGRAM.
50
60 REM: I HAVE TRIED VARIOUS COMBINATIONS OF OPENOUT, OPENUP,
70 REM: OPENIN etc. and still cannot get it to work. Help please.
80 REM: Also see my program "TEST FILE HANDLING13" in which I try ro retrieve this file.
90 REM----------------------------------------------------
100
110 DIM GIN(12)
120
130 X=0
140 A=0
150 REPEAT
160 X=X+1
170 A=A+3
180 GIN(X)= A
190 PRINT"GIN(";X;") = ";GIN(X)
200 G$=GET$
210 UNTIL X=10
220 PRINT"GIN(X) PRINTING FINISHED"
230 REM: GIN(X) stands in for the already-created file N(X,Y,Z) in the program SUDOKU80.
240 REM---------------------------------------------------------------------------------
250 REM: NOW I WILL TRY TO SAVE THE FILE "GIN", AS "GinFile".
260 PRINT""
270 GinFileNum%= OPENOUT ("GinFile.dat")
280 REM: "GinFile" is the name I have given to my newly created file.
290 REM: GinFileNum% is my name for the number the computer has assigned to that new file.
300 PRINT"GinFileNum%= ";GinFileNum%
310 REM: This returns the channel number 5.
320 PRINT"AFTER OPENOUT GinFile"
330 M=0
340 L=OPENIN"GinFile.dat"
350 REPEAT
360 M=M+1
370
380
390 PRINT"M= ";M
400 UNTIL M=10
410 PRINT"PRINTING FINISHED"
420 END
==================================
10 REM: THIS IS TEST FILE HANDLING13, a change to TEST FILE HANDLING11; IN ST 50 I HAVE ADDED .dat AFTER ("GinFile
15 REM: BUT I STILL GET ALL Found(F)=0.
20 REM: I AM TRYING TO RETRIEVE THE FILE I TRIED TO SAVE IN PROGRAM "TEST FILE HANDLING12".
30 REM: THIS DOESN'T WORK. IT GIVES ALL Found(F) =0. What am I doing wrong?
40 DIM Found(20)
50 fnum = OPENIN("GinFile.dat")
60 PRINT"fnum = ";fnum
70 IF fnum=0 THEN PRINT "No GinFile data.":END
80
90 F=0
100 REPEAT
110 F=F+1
120 INPUT#fnum,Found(F)
130
140 PRINT "Found(";F;")= "; Found(F)
150 REM: THESE SHOULD BE MULTIPLES OF 3, BUT ALL=0. WHY??
160
170 UNTIL F=12
180 CLOSE#fnum
190 PRINT"INPUT FINISHED"
200 END
-========================-===========
====================================


User IP Logged

rtr2
Guest
xx Re: Moving and array between programs
« Reply #11 on: Oct 27th, 2014, 10:56pm »

on Oct 27th, 2014, 10:37pm, chrispc wrote:
I am sorry to be such a nuisance, but am comforted by seeing the experts finding it tricky too.

It is not in the least 'tricky'. I explained the problem, and its solution, in my replies. If you make the changes I suggested your program will work. Specifically:
  1. Delete line 340, which is spurious.
  2. Change line 380 to PRINT #GinFileNum%, GIN(M)
That is all you need to do.

Richard.
User IP Logged

chrispc
New Member
Image


member is offline

Avatar




PM


Posts: 19
xx Re: Moving and array between programs
« Reply #12 on: Oct 28th, 2014, 02:50am »

Thanks very much. That seems to work.
User IP Logged

Edja
Developer

member is offline

Avatar




PM


Posts: 60
xx Re: Moving and array between programs
« Reply #13 on: Oct 28th, 2014, 11:02am »

Quote:
You say about statement 300: “Overhere GinFileNum% equals 0…”
I've copied your program from your forum message and executed it without first saving it. As a result GinFileNum% equals 0. Nothing to do with your coding and entirely my error (see also Richard's explanation)
Quote:
changing line 50 from fnum=OPENIN(“GinFile”) to fnum=OPENIN(“GinFile.dat”) made no difference to my result
Yes it does.You've made this correction (before adding Richard's corrections) and it didn't work because in the first program you did create the file GinFile.dat but left it empty. When you then tried to read from it with your second program it executed correctly but without finding any data in the file. Therefore the values shown were all equal to zero.

Now that you've applied the corrections that Richard correctly suggested the first program does write to the file and the data that you read with the second program are no longer zero.

As a final check (only for your better understanding) replace line 50 fnum = OPENIN("GinFile.dat") again to 50 fnum = OPENIN("GinFile") but keep Richard's corrections in the first program. Then run both program's to see that the second one won't open the file anymore.

I hope this leads to a better understanding, certainly not adding to any confusion.

Eddy
User IP Logged

rtr2
Guest
xx Re: Moving and array between programs
« Reply #14 on: Oct 28th, 2014, 12:22pm »

on Oct 28th, 2014, 11:02am, Edja wrote:
As a final check (only for your better understanding) replace line 50 fnum = OPENIN("GinFile.dat") again to 50 fnum = OPENIN("GinFile") ... see that the second [program] won't open the file anymore.

I'm not keen on the idea of introducing a deliberate fault just to observe that it causes the program to fail. In any case the effect may not be what you describe - suppose one of the programs has been saved with the name 'GinFile' (i.e. as the file GinFile.bbc) or that a data file 'GinFile' has previously been created by an earlier version of the first program. Either way the modified code will open the file but it will report the wrong results!

Quote:
I hope this leads to a better understanding, certainly not adding to any confusion.

I fear it is more likely to confuse than to educate!

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls