Writing to the /var/tmp directory in C++ - 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.

Related

Opening a shared file in C++ without root previliges

I am working on a Red-Hat machine without root prevliges.
I have some shared files that I can access using smb:// with username and password. Since I don't have root previliges, I can't mount the share (or at least, I haven't been able to find out how).
What I need is to open the file for reading\writing from a C++ code.
Is there any way to do so?
you can use libsmbclient library to do normal file access like smbc_open, smbc_fstat, smbc_lseek, smbc_read.

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

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.

How to open a file in write mode which is in ROOT?

I have a file in the root directory which I want to open for editing in my QT application.
Generally we use sudo filepath to open the file with all permissions.
But i want to achieve this in my c++ QT application.
Any kind of help is appreciable.
Thank you.
Do the following.
Get userid, if it is not 0 (uid of root is 0), the do the following
Execute argv[0] with gksu/kdesu.
Perform tasks.
Your program needs to be executed with root privileges, or, more specifically, it needs to be executed as a user who has access permissions for those files.
If it is not, then it is impossible to access those files, because the OS will not allow your program to do so. There are no changes in the C++ code that will fix this. You need to execute your program using sudo.

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.