BBC BASIC for Windows
General >> General Board >> "Open" Window, selecting a file
http://bb4w.conforums.com/index.cgi?board=general&action=display&num=1257792752

"Open" Window, selecting a file
Post by Danny H on Nov 9th, 2009, 5:52pm

Hello
Could someone let me know how I can
get the Open window to appear, then to select a .txt file
and for that filename to be stored as a string.
Many thanks for your help.
Re: "Open" Window, selecting a file
Post by Michael Hutton on Nov 10th, 2009, 09:17am

Danny,

The code is in the manual. Look under "Accessing the Windows API - Using dialogue boxes - File Open and file save".

I know the code looks a bit overcomplicated but once you get your head around it it is a very powerful way of interacting with windows.

Michael


Re: "Open" Window, selecting a file
Post by admin on Nov 10th, 2009, 09:26am

Quote:
The code is in the manual.

I'd recommend the code here instead:

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwine.html#opensave

The difference from the code in the 'local' manual is that the filename buffer is allocated differently, eliminating the risk of a memory leak if the code is executed multiple times.

Richard.
Re: "Open" Window, selecting a file
Post by Danny72 on Nov 10th, 2009, 09:40am

Thanks very much, got it now.
Danny
Re: "Open" Window, selecting a file
Post by Michael Hutton on Nov 10th, 2009, 10:13am

Quote:
eliminating the risk of a memory leak if the code is executed multiple times.


Code:
      DIM A% 259
      PRINT A%
      DIM A% 259
      PRINT A%
      DIM A% 260
      PRINT A%

      DIM fp{t&(260)}
      PRINT fp{}
      DIM fp{t&(260)}
      PRINT fp{}
      DIM fp{t&(261)}
      PRINT fp{}

 


I hadn't thought of that before. Only the last statement gives a 'Bad DIM Statement' Error' As does DIM fp{t&(20)}.

So does BB4W check the heap when DIMing a structure or an array but not a variable?

Michael
Re: "Open" Window, selecting a file
Post by admin on Nov 10th, 2009, 11:00am

Quote:
So does BB4W check the heap when DIMing a structure or an array but not a variable?

I don't entirely understand the question, but maybe the following will help:

Code:
      DIM buf% size% 

This statement allocates a block of memory and assigns its address to the variable buf%. What's important is that buf% is a regular (integer) variable; nothing about the variable or its value records the fact that it points to a memory buffer, nor that its value was assigned in a DIM statement. Only you know that!

So if another similar DIM statement is executed, BBC BASIC can't possibly know to do something different from what it did the previous time. It doesn't even know there was a previous time! Hence each time the statement is executed another block of memory is allocated (potentially a memory leak).

This is fundamentally different from the 'conventional' use of DIM to declare an array or a structure, where the object created itself records the fact that it was created using DIM, how big it is etc. Now, if the statement is repeated, BBC BASIC has the information available to allow it to make different choices (such as re-use the original memory or issue an error message).

Richard.