Save data in a specific directory

Discussions related to database technologies, file handling, directories and storage
Ivan
Posts: 127
Joined: Tue 07 May 2019, 16:47

Save data in a specific directory

Post by Ivan »

Trying to save in a specific directory and got an Invalid Channel error.

I can change the default directory, but saved files will still be saved in windows user directory but I prefer to save in a specific directory.

Will the code below be doable in some form?

Code: Select all

        Drive$ = "c:"
        Path$  = "\test"
        File$  = "try"
        Ext$   = ".ing"

        projekt$ = Drive$ + Path$ + File$ + Ext$
        A = openout(@usr$ + projekt$)  
        print#A, stk_{(1,1)}  
        close#A
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. C++ in 2021.
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Save data in a specific directory

Post by hellomike »

Hi,

Well, I did not test it but in Projekt$ you just concatenate the 4 part to form one string.

I think Projekt$ would contain "c:\testtry.ing"..... (NOT "c:\test\try.ing").

Then you add this string to the string that is in @usr$..... In my environment @usr$ contains "C:\Users\Hello\Documents\" so @usr$ + Projekt$ would come to:

"c:\testtry.ingc:\Users\Hello\Documents\".

Maybe instead you should do: @usr$ + File$ + Ext$

Hope this helps.

Mike
RichardRussell

Re: Save data in a specific directory

Post by RichardRussell »

Ivan wrote: Mon 05 Oct 2020, 19:39 Trying to save in a specific directory and got an Invalid Channel error.
I'm guessing because the directory C:\testtry doesn't exist (OPENOUT does not automatically create directories). Did you mean C:\test\try?

Printing projekt$ would probably have been helpful for debugging.
RichardRussell

Re: Save data in a specific directory

Post by RichardRussell »

hellomike wrote: Mon 05 Oct 2020, 19:56In my environment @usr$ contains "C:\Users\Hello\Documents\" so @usr$ + Projekt$ would come to:

Code: Select all

"c:\testtry.ingc:\Users\Hello\Documents\"
No it wouldn't, because the concatenation adds @usr$ to the beginning, not the end! I've no idea why @usr$ was there at all.
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Save data in a specific directory

Post by hellomike »

You are correct, wrong order. :mrgreen:

And yes, @usr$ is not needed.
Ivan
Posts: 127
Joined: Tue 07 May 2019, 16:47

Re: Save data in a specific directory

Post by Ivan »

You directed me in the right direction - thanks!

The test folder existed and "everyone user" have all access.

I got the "@usr$" from an example and I wrongly assumed I had to use it to access files.

From the manual: "@usr$ The user's Documents directory (folder)"

I removed "@usr$" and corrected the path.

This code seems to work. I can now save the structure to a file and load it again with additional code.

Code: Select all

      Drive$ = "c:"
      Path$  = "\test\"
      File$  = "try"
      Ext$   = ".ing"

      projekt$ = Drive$ + Path$ + File$ + Ext$

      print "Debug: ";projekt$

      A = openout projekt$

      print#A, stk_{(1,0)}
      close#A
Thanks all.
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. C++ in 2021.
RichardRussell

Re: Save data in a specific directory

Post by RichardRussell »

Ivan wrote: Tue 06 Oct 2020, 08:39 From the manual: "@usr$ The user's Documents directory (folder)"
Do you consider that to be at all confusing or ambiguous? @usr$ and the related system variables (@tmp$, @lib$, @dir$) each contain the full (absolute) path to various useful directories/folders. Because they contain absolute paths, i.e. starting with the drive letter in Windows or with a leading oblique stroke on other platforms, it does not make sense to concatenate them with another full path. On the other hand concatenating them with a relative path to reach a sub-directory is common.

@usr$ contains the path to the user's Documents folder: it is guaranteed to be writable and to be specific to the current user, so for example is a good place to store the current state of a game. @tmp$ contains the path to a folder where files may be stored that are no longer required once the session is ended. @lib$ contains the path to the directory containing the BBC BASIC libraries, you should assume it is read-only. Finally @dir$ contains the path to the current BASIC program (usually: it won't if you have navigated to it in immediate mode), again you should assume it is read-only.
RichardRussell

Re: Save data in a specific directory

Post by RichardRussell »

RichardRussell wrote: Sat 10 Oct 2020, 13:14@usr$ and the related system variables (@tmp$, @lib$, @dir$) each contain the full (absolute) path to various useful directories/folders.
I suppose I should add, if it isn't obvious, that the purpose of these system variables is to make it easy to write programs which will run on any platform, without needing to know the details of how the file system is organised. As soon as you reference an absolute file path, on your local machine, you lock the program to that particular environment.
Ivan
Posts: 127
Joined: Tue 07 May 2019, 16:47

Re: Save data in a specific directory

Post by Ivan »

"Do you consider that to be at all confusing or ambiguous? "

At the time I took the example I did not realize that usr was a system variable for a user's directory as I explained. But yes, I misunderstood lots and lots of things because of misinterpretations hence my dyslexia and even simple things now and then confuses me.

As I rewrite programs or reuse parts from my old UniComal I often struggle with variable names that starts with reserved words in BBCBASIC. I had changed the colour of reserved words and it helps.

I think predefined variables, prototyping and strong typed variables and more indention are something I have to consider in the future.

The inner logic in my programs almost goes well all the time - it's always the syntax and misreading that takes most time.

It's intentionally that I have static paths so I have as little dependency of system variables as possible.

In case you had forgotten: I spend a lot writing to you and explained about my troubles of reading in a long PM not a long time ago so I was surprized about your answer.
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. C++ in 2021.
RichardRussell

Re: Save data in a specific directory

Post by RichardRussell »

Ivan wrote: Mon 12 Oct 2020, 09:28In case you had forgotten...
Indeed I had forgotten. My Alzheimer's disease (probably; tentatively diagnosed after a brain MRI scan but they can't be sure until post-mortem) results in me having a very poor, and worsening, memory. It is disconcerting, because I can sometimes remember things that happened years ago but not things that happened yesterday!

So you will typically find that my replies don't make reference to, or take account of, anything that went before that I cannot easily review. I will often read the thread from the beginning to remind myself of the context, but if relevant information is elsewhere (e.g. in a PM, as in this case) I won't refer to it because I won't remember it.

As on this occasion, this is often interpreted as rudeness or lack of consideration. I have twice absented myself from this forum for several months, I left the Discussion Group for about a year, and I recently resigned from the Stardot forum. In every case it is because my behaviour has caused annoyance and irritation. Would you like me to leave?

None of this is intended as an excuse, and I am well aware that nobody has the slightest sympathy. It is known that I nearly died in the womb (I have a pre-natal arachnoid cyst that would normally have been fatal) and it would have been better for everybody if I had.