Whenever I try to create a file using fopen, fopen acts as if the file has been opened properly and that it has full access to it, but it doesn't actually create the file. My program doesn't have write access to the system root folder because it needs admin access to write there, but why isn't fopen() giving any errors?
Any idea how to tell if there's an error or not? The file handle that gets returned when I'm trying to open the file in a protected directory is exactly the same as when I open a file in a directory where I have write access.
I've tried this with the various different versions of fopen (fopen, _wfopen, _wfopen_s) but they all have the same output.
Interestingly enough, GetLastError() returns ERROR_ALREADY_EXISTS.
Here's the code that I'm using:
FILE *FileHandle;
DWORD error = _wfopen_s(&FileHandle, L"\\filename.txt", L"a");
Win32Error = GetLastError();
if (error != 0 || FileHandle == NULL)
{
//Throw error
}
else
{
//write to file
//close file
}
EDIT: As rerun pointed out, the file is getting created in %APPDATA% because of virtualization. Does anyone know how to disable this feature?
The fopen function creates the file if it does not exist.
The fopen() function opens a file indicated by fname and returns a stream associated with that file. If there is an error, fopen() returns NULL. mode is used to determine how the file will be treated (i.e. for input, output, etc) Mode.
r or rb. Open file for reading. w or wb. Truncate to zero length or create file for writing.
Returned value If successful, fopen() returns a pointer to the object controlling the associated stream. If unsuccessful, fopen() returns a NULL pointer.
This might have to do with the virtual store. Take a look here for some info.
If you are literally running the code you posted, then you aren't asking for a file in the root directory at all. You need to go "c:\\filename.txt"
(two '\' characters) instead. Maybe if you look in the current directory you'll find you've successfully created a weirdly named file (basically <formfeed>ilename.txt
, I think).
We had a similar problem in registry virtualization, yes it is also virtualized after Vista.
Our solution was Properties->Linker->Manifest File->UAC Execution Level-> requireAdministrator. IMHO, It may work for your case also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With