Please consider the following: creating files in your posts

Everything else that doesn't fall into one of the other PB categories.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Please consider the following: creating files in your posts

Post by luis »

When you post some code in coding questions or in the bug sections which creates a file I would suggest to put the file in the temp directory if it's possible.

You can do it this way

Code: Select all

f$ = GetTemporaryDirectory() + "testfile.txt"

f = CreateFile(#PB_Any, f$) 
Your code may not work if you don't put your file in temp, because the creation of the file may fail due to insufficient rights.

This will fail on my computer:

Code: Select all

f = CreateFile(#PB_Any, "testfile.txt") 
Because when I copy/paste your code to the IDE and then run it the current dir in my case will be "C:\Program Files (x86)\PureBasic\" and the process can't write in there.

To make it work I have to modify it, or save it to a directory with the right permissions since in this case the current directory will be set by PB to the source directory and your CreateFile() which relies on the current directory will work.

But when I'm testing code from the forum, I just copy/paste/run it without saving it if I can avoid it.

Also your file may overwrite a file already there with the same name, and again this problem is mitigated if you use the temp dir.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Please consider the following: creating files in your po

Post by mk-soft »

I agree :wink:

Perhaps some time better

Code: Select all

f$ = GetUserDirectory(#PB_Directory_Documents)
Debug f$
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Please consider the following: creating files in your po

Post by luis »

That might be ok too, but I personally prefer systemp for the usage described above because it's more "disposable".
Basically what goes there can be safely thrown away from time to time, not so much with other user-related directories where you should be more cautious.

But that's just my opinion. :)
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Please consider the following: creating files in your po

Post by Olliv »

Hello Luis and mk-soft,

I am interested by such a rule I should apply in the way where I post a code recording one or many files.

Just a question about an other suggesting :

As today is the 3rd, does a path like :

Code: Select all

{Root}17/07/03/
work rightly without problem of right? Or is it source of problem?
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Please consider the following: creating files in your po

Post by mk-soft »

Never write direct into root folder.
Never use speziale character. the character "/" is a same as "\" for Mac and Linux
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Please consider the following: creating files in your po

Post by Olliv »

I apologize. My rights was on squint mode, what it swaps right and left.

On Windows

Code: Select all

FaultPath.S = "\17\07\03\"
On Linux

Code: Select all

FaultPath.S = "~/17/07/03/"
(For 'July, the 3rd, 2017')
Does this cause any problems of rights? Because this time-sort is very simple. And there is just a count of directory equal to the count of years recorded on the root path.

I never asked myself this question of rights...
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Please consider the following: creating files in your po

Post by luis »

@Olliv

My intended scope about this thread was limited to the code posted in coding questions or the bug sections, so I was not talking about generally accepted good practices in a real program.

For example this bug report -> http://www.purebasic.fr/english/viewtop ... 38#p507938 it's one I can't successfully test as it is, for the reasons I mentioned in this thread.

My point was: I can manually fix it, but if you post it using the guidelines I suggest, it would be better for your audience in the first place.

Everyone can do as he please, but doesn't mean there is not a better way to do it :wink:


BUT ... if I understood correctly, you are instead asking about good practices in a real program.

In your example above, you are referencing a path in the user home on linux, but the root of the current drive on windows (usually not a good idea)

Why the difference ?

PB recently (I think) added the function GetUserDirectory() (mentioned by Thomas a little above) to give you a consistent place where to put the files created by your programs in a crossplatform way, give a look at that and I think you may choose the most sensible directory for your files from there.
In there, you should not encounter "rights" problems.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Please consider the following: creating files in your po

Post by Olliv »

Effectively, there is a large choice of standard cross-platform directories within this native function.

It will be easier to share a file problem.

Thank you, for this constructive discussion.
Post Reply