C++: Copy a file to the root directory under Windows - c++

I wonder whether it is possible in C++ to programatically copy a concrete file to the root directory. For example, say I have a file exm.txt in the location C:\example\ and I'd like to copy it to C:\ so that I'll obtain a file C:\exm.txt. Is it possible in C++ WinAPI? When using
CopyFile("C:\\example\\exm.txt","C:\\exm.txt",true);
nothing happens and this functions returns error code 5: Access denied [I'm almost sure I'm working as the administrator - this is my personal computer].
The aforementioned function - as far as I know - works correctly for all other directories (different from the root directory in some partition). So, my question is whether we can do programatically copy also to the root directory.
Thanks in advance for any help!

That is because the security settings, by default, do not allow standard user to create files at the root level of the system drive.
The obvious solutions are:
Save the file somewhere else if you can. It's good practise not to litter the root directory of the system volume.
Run the process with elevated rights by adding the requireAdministrator option to your manifest. Or by right clicking the executable or shortcut and selecting Run as administrator.

Related

Qt. All functions work in release folder, but not in Program Files

in my program I have functions to restart program and load / save settings from .ini file. Everything is working in release folder, but when I install it (program is in C://Program Files (x86)):
*When I call restart function
QProcess::startDetached(QApplication::applicationFilePath());
exit(0);
Application is terminating itself, but new instance is not started...
*or When I call save settings to .ini file
QSettings setting(qApp->applicationDirPath() + "//config//settings.ini", QSettings::IniFormat);
setting.sync();
setting.beginGroup("/Example");
setting.setValue("/Example1",
setting.endGroup();
When I run App first time, usually It saves once, but next times Settings is not saved, settings.ini, or any other .ini file which I'm trying to save looks like 'locked' and I can't save it even through notepad... But If I run App with Administrator permissions everything is working (Restart still don't)
//Edit: I'have checked this on desktop, It works. On another partition (D://) it doesn't work.
In this moment other functions work normally. Could anybody help me with this issue?
i think you need admin rights to create a file to Program Files in latest windows. Try install it somewhere else and check if it still fails
By default you need administrator rights to write inside Program Files.
Your application should use instead the path C:\ProgramData (assuming C: system drive), to save files intended to be shared with all users (eventually creating a directory inside with the company / application's name). The location should be accessed by using the environment variables ProgramData or AllUsersProfile.
Or, to save settings / data separately for each user, you could use these environment variables to access the intended directory:
AppData (expanding to e.g. C:\Users\<Username>\AppData\Roaming) - user profile specific data
LocalAppData (expanding to e.g. C:\Users\<Username>\AppData\Local) - machine specific data
getenv function can be used for reading an environment variable.

Writing to the /var/tmp directory in C++

I am using Ubuntu and I am trying to create a new file in the /var/tmp directory in c++ but for some reason it is not creating the file. Also, how long do files last in this folder usually?
Here is what I have:
string defaultPath = "/var/tmp/a.txt";
ofstream ouputFile(defaultPath);
I think it is something with "/var/tmp/" I am not sure because I am not that familiar with linux operating systems. Any help is much appreciated.
Most likely this question is unrelated to C++, you should check if user, which runs the program has permissions to create a file in /var/tmp:
touch /var/tmp/a.txt
Then check if file is created there. If not check permissions on /var/tmp and fix them by chmod or choose directory where the user has write permissions.

Deploying an executable with a configuration file

I'm new to deploying programs written in C/C++ on Linux and I'm wondering what you'd do in this situation.
I have a binary file (compiled with GNU Make) that needs to read a config file (such as myprogram.conf). But when I write a Makefile to deploy this file to /usr/bin/, where should the config file go? And how does the executable know where it is?
You have endless options, but the best way depends on a couple of things. First, is it a user-specific configuration file, or is it global to all users?
If it's user specific, you could, for example, keep it in ~/.myprogram/config.file and have the program check there. As a service to your users, it's up to you to decide what to do if it's not found -- perhaps copy a default config there from somewhere else, or generate a default, or use hard-coded default options, or display a configuration wizard, or just fail. That's entirely up to you.
If it's global, the traditional place to put it on Linux is in /etc, e.g. /etc/config.file or /etc/myprogram/config.file. See Linux File System Structure. You will generally always have a /etc on Linux. Handling a situation where the file does not exist is the same as above - there's no "right" way to handle that, it's based purely on how convenient you want to make it for a user.
What I usually do for global config files is put them in /etc/wherever on install, have the program default to loading the config file from /etc/wherever, but also give a command line option to override the configuration file (especially useful for testing or other situations).
What I usually do to handle missing config files depends entirely on the application. I'll generally either have hard-coded defaults (if that's appropriate) or simply fail and direct the user to some documentation describing a config file (which I find adequate in situations where my installer installs a config file).
Hope that helps.
It kind of depends on what the configuration parameters are, and whether they are "per system" or "per user" or "per group" or ...
System configurations typically live somewhere in /etc/.... In the same directory that the program lives is a very good place too.
User confgiurations, in the home directory of the user.
Group configurations are the trickiest, as you'll probably need to come up with a scheme where there is a configuration file per "group". /etc/myprog/groups/<groupname>/config or something similar would work.
On Linux, the usual location for configuration files is '/etc', so it is acceptable to deploy a configuration file like /etc/myprog.conf. That requires root privileges however. Other good options include putting a configuration file in the user's home directory, making it something like ~/.myprog.conf or ~/.myprog/.conf to use a folder where you can have several config files, a cache or something else that you want.
As for how the executable knows where the file is, one solution is to look for the file in several common locations. For example, if you decided to place your config in the user's home directory, look for it there first, if not found, look under /etc. And allow a special command line argument that would let a different config file to be loaded. So, say, an invocation of myprog can check for a config file in the home folder, but myprog -c /some/path/config will use /some/path/config as the file. It's also a good idea to have some default settings that you can fall back to if there is no valid config file anywhere.
The config file can go anywhere, but I'd try to put it in the same directory as any other files the program will read or write.
As for how the executable will find it, I'd pass the config file's path to the executable on the command line as an argument, with a default value of "." (which is the current directory, the one you're in when you launch the executable).

File I/O from current Windows position C++

I have not yet found a definitive answer about this. I am trying to have access to files in subfolders from my .EXE. When I have asked before, people tell me to use the absolute location i.e. "c:/game/info/" if I wanted to access something in /info/
But it is completely unreasonable for me or anyone to assume that someone is going to use their program from the same directory. What if the user only has a D drive? That sort of thing.
So my question is: how can I access a file in a subdirectory from my executable without relying on the entire path?
Your title says "Windows", so I'll give a WinAPI-specific answer.
On Windows, you can find your application directory with GetModuleFileName(NULL, ...), and PathRemoveFileSpec. Then PathAppend will make the full path to your data files.
Or you can store the data inside you .exe file as Win32 resources, so they never get separated.
Please note that this approach generally works only for read-only access to data files. If you try to write files in your application directory, you might be blocked by ACLs (depending on install location and local security settings of the computer).
Use GetModuleFileName (Retrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.)
char strExePath [MAX_PATH];
GetModuleFileName (NULL, strExePath, MAX_PATH);
You'll then need to extract the folder path (someone has already posted how to do that), and combine your path.
Make or use an installer that asks the user where to install the executable and writes that path to the registry in a well-known location for later reference.
if you use:
#include <fstream>
ifstream stream("file");
it will be working. "file" is file in directory with your exe. Of course if you want go up or down in folders hierarchy use "..\file" or "folder\file"

Access denied when updating config file in ProgramData directory. How do I fix this?

I can't seem to update my configuration file in c:\ProgramData\appname\config.ini. I keep getting an access denied error. Seems the ini code fails to be able to delete the existing .ini file when trying to update it with the new file. Funny thing is that I have other programs I've written that share the same code but don't have this problem. This is troubling.
Comparing my working program with this non working program, I noticed that when I click on the security tab for the ini file, I see that my user name [Scott] is not listed under "Group or Usernames" like in the working program's ini file. Also, the non working one has a shield next to Delete and Rename (when right click on filename) while the working one does not have this. How can I set the permissions? The setup file for both projects is identical and when I create the folder and copy the program file in Isso Setup, no permissions are set (read only flag is not set, hidden is not set, and System is not set).
Why am I getting this and how come my other program install and works fine? Installer runs as administrator and does the access permission prompting. What do I need to do?
This is because your program was not run as an administrator. You have to either run your program as an administrator or move your .ini file and any other files you wish to update during the duration of the program to a location like C:\Users\\AppData\Local\. Note that for the latter option you will have to update your program likewise.
To run your program as an administrator right-click on ur program (or shortcut) and click Run as Administrator.