Page 1 of 1
Please consider the following: creating files in your posts
Posted: Sat Jun 24, 2017 2:06 pm
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.
Re: Please consider the following: creating files in your po
Posted: Sat Jun 24, 2017 4:15 pm
by mk-soft
I agree
Perhaps some time better
Code: Select all
f$ = GetUserDirectory(#PB_Directory_Documents)
Debug f$
Re: Please consider the following: creating files in your po
Posted: Sat Jun 24, 2017 4:28 pm
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.

Re: Please consider the following: creating files in your po
Posted: Mon Jul 03, 2017 2:12 am
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 :
work rightly without problem of right? Or is it source of problem?
Re: Please consider the following: creating files in your po
Posted: Mon Jul 03, 2017 7:30 am
by mk-soft
Never write direct into root folder.
Never use speziale character. the character "/" is a same as "\" for Mac and Linux
Re: Please consider the following: creating files in your po
Posted: Mon Jul 03, 2017 8:39 pm
by Olliv
I apologize. My rights was on squint mode, what it swaps right and left.
On Windows
On Linux
(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...
Re: Please consider the following: creating files in your po
Posted: Mon Jul 03, 2017 9:35 pm
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
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.
Re: Please consider the following: creating files in your po
Posted: Tue Jul 04, 2017 9:29 pm
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.