Saving file problem

Just starting out? Need help? Post your questions and find answers here.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Saving file problem

Post by doctorized »

I have the following code to write the elements of a listgadget to a file.
If the path is "C:\" the file is not created (Win 7 x64 Ultimate) but OpenFile() returns nonzero.
If FileSize() is called in the end, nonzero is returned but the file is nowhere. What do I do wrong?
I know that the OS is not giving me permission to write on "C:\" but why PB 5.30 is telling me that the file is writen successfully?
How can I find out if the file is really writen?

Code: Select all

Procedure SaveFile()
Pattern$ = "Text (*.txt)|*.txt|All files (*.*)|*.*"
Pattern = 0
File$ = SaveFileRequester("Please choose file name", "LAN Info", Pattern$, Pattern)
If File$
	If ReadFile(0,File$);file already exists
		CloseFile(0)
		i=MessageRequester("Warning","File already exists. Overwrite ?",#PB_MessageRequester_YesNo)
		If i= #PB_MessageRequester_No			
			ProcedureReturn
		EndIf
	EndIf
	If GetExtensionPart(File$) = ""
		File$ + ".txt"
	EndIf
	ii = OpenFile(#PB_Any,File$)
	If ii
		For i=0 To CountGadgetItems(0)
			tmp$ = GetGadgetItemText(0,i,0)
			If tmp$ = ""
				WriteStringN(ii, "")
			ElseIf FindString(tmp$,"Connection  #") > 0
				WriteStringN(ii,tmp$)
			Else
				WriteStringN(ii,LSet(tmp$,36," ") + " : " + GetGadgetItemText(0,i,1))
			EndIf
		Next
		CloseFile(ii)
		MessageRequester("","File created successfully.")
	Else
		MessageRequester("","Cannot create file.",#MB_ICONERROR)
	EndIf
EndIf
EndProcedure
deesko
User
User
Posts: 39
Joined: Fri Sep 21, 2012 11:40 pm
Location: Portugal

Re: Saving file problem

Post by deesko »

Can you try giving your program administrator priveligies and see if you get different results?
User avatar
Bisonte
Addict
Addict
Posts: 1329
Joined: Tue Oct 09, 2007 2:15 am

Re: Saving file problem

Post by Bisonte »

Compile it with "User" privilegs, and see if it works...

Normally if you have adminprivilegs, you can write all over, and in "normal" mode, UAC simulate privilegs... (Virtualization).
So the User - Mode have in the result the right value.
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Saving file problem

Post by doctorized »

Turning user mode on, after pressing the "save" button, a window pops up telling me that I do not have rights to write here.
Turning admin rights on the file is created successfully. This annoys me a little as I have to use admin rights in order to be sure that the code is working as it should be. OpenFile() should inform me that the file cannot be opened. FileSize() should not return a nonzero value as no data are really writen. Is it a bug or I think so?
deesko
User
User
Posts: 39
Joined: Fri Sep 21, 2012 11:40 pm
Location: Portugal

Re: Saving file problem

Post by deesko »

Is the filesize value the same even if admin is turned off (besides being nonzero)?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5032
Joined: Sun Apr 12, 2009 6:27 am

Re: Saving file problem

Post by RASHAD »

Search "C:\Users\???(the user name)\AppData\Local\VirtualStore" to see if your file is there or not
Probably you need to enable seeing the hidden files and directory for Explorer to be able to see "AppData"
Egypt my love
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Saving file problem

Post by doctorized »

deesko wrote:Is the filesize value the same even if admin is turned off (besides being nonzero)?
Yes, it is the same.
RASHAD wrote:Search "C:\Users\???(the user name)\AppData\Local\VirtualStore" to see if your file is there or not
Probably you need to enable seeing the hidden files and directory for Explorer to be able to see "AppData"
Yes, the file is in this folder when admin rights are off. But i still insist, is it a bug or not as I check file size of a file in "C:\" and not in VirtualStore.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5032
Joined: Sun Apr 12, 2009 6:27 am

Re: Saving file problem

Post by RASHAD »

PB checks about the exciting of the file through the Operating System
Is it there ? Yes
Due to dealing with 3 OS it will be extra work to check thoroughly by itself
So you can report to the request forum maybe :)
Egypt my love
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Saving file problem

Post by doctorized »

RASHAD wrote:PB checks about the exciting of the file through the Operating System
Is it there ? Yes
Help me understand one thing please. You mean that PB is looking for the file all over the disk?
When I give a file in FileSize() is the procedure looking for the file in many locations other than the path I gave in the case the file is not there?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Saving file problem

Post by PB »

> You mean that PB is looking for the file all over the disk?

No, it doesn't. It only looks where you specify.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
HeX0R
Addict
Addict
Posts: 1242
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Saving file problem

Post by HeX0R »

Ticking User Mode should be the correct method, this will inform your program, when it doesn't have writing rights and the user should select another path.
Unticking user mode, will create a not-windows7-compatible executable, so windows thinks it is an old program meant to be running under win xp.
Therefore windows simulates a writing permission to folders, where in fact it hasn't any (and will store it in the virtual store).

In this mode, you can still use openfile, readfile, createfile, filesize, [...] because windows will automatically redirect.
So all of your file actions will still work, but the file is somewhere else (but who cares?).

Usually you should select user mode by default.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Saving file problem

Post by doctorized »

HeX0R wrote:So all of your file actions will still work, but the file is somewhere else (but who cares?).
I care and the users will care if they save a file using a program of mine and when they try to find the file to open it or send it and the file will be absent!
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Saving file problem

Post by citystate »

I think the problem is just location, you should be able to save somewhere like GetHomeDirectory() or GetTemporaryDirectory(), just not in C:\

of course, that doesn't address the issue of not getting a 0 result during file creation...
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Saving file problem

Post by doctorized »

citystate wrote:I think the problem is just location, you should be able to save somewhere like GetHomeDirectory() or GetTemporaryDirectory(), just not in C:\
It is not just me, it is what path will someone choose to save the file in a program like this: http://purebasic.fr/english/viewtopic.p ... &start=105. In this case admin rights are needed to run the app so we have no problem here but is some other cases admin rights are not needed so I must take extra precautions to be sure that the file will be created where the user wants to and not somewhere else.
Post Reply