Opening a shared file in C++ without root previliges - c++

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.

Related

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.

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.

How to reference a file to be opened in C++ given that its full path name will change from computer to computer?

Our Computer Science teacher has given us a project to make a fully functioning console application using C++. And I have started to make it. But I got stuck at some point. I want to open an editable text (.txt) file using the open() function. But I made a separate folder for all the text files. Usually I have to provide a full directory path in the open() function, which is F:\Work\C++\SchoolProject\TextFiles in my case. But what if I copy the SchoolProject folder in a portable drive and take it to my friend's home and try to run the program in their computer. Will it work? I'm asking because it is not necessary that they will have the Work folder in the F directory or maybe they may not have the F disk at all. So in that case the path will change. So what path I have to type in the open() function so that the program works in each and every computer without changing the address in the open() function every time I try to run the program in some other computer. A source code may be helpful with explanation. Thank You!
Instead of using absolute paths, you should use relative paths. When you run your program from a folder, this is your working path. You can then open files inside this folder or subfolders of this folder by passing only the file name or folder and file name to the open function. So instead of opening C:\... simply open someFolder\someFile.txt.
You could consider having the filename that you parse in as part of a command line argument, like this:
int main(int arg, char* args[]) {
FILE *newfile = fopen( args[1], "r");
}
You can not be sure all computers have the F: drive mapped correctly so it is better to use
Universal Naming Convention (UNC) names i.e. "\server\share\path\file".
A nice way to achieve the same is by using Boost Filesystem, but this makes your code more complicated since you are depending on an external library (read: the students might be confused). The documentation for Boost Filesystem is found here: http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/index.htm

I want to force file open when directory is not exist

When i open file by the next way:
ofstream outputFile(Filename, ios::out | ios::binary);
i gets error result when the directory not exist:
temp\filename.ext
when the directory exist it work well.
I want to know how can i force the operation in c++ command
You can't. There is no standardized way to create a directory in c++.
You might want to take a look at the filesystem library from boost. There are multiple functions for checking if the directory/file exits and if not how to create them.
The file is created automatically when creating an ofstream if it does not exist. But the directory where you want this file to be created must already exist.
There is no way to create a directory automatically in C++. You have to create the directory in C++.
Furthermore, and unfortunately, there's no C++ standard way of dealing with directories. You'll have to revert to the C system library mkdir of your operating system to create it.
I'm the first to admit I know nothing about C, but in any programming language trying to force an open file operation on a file that doesn't exist is a BAD THING!
Check if the file you're trying to open exists. If it doesn't, create it. If you can't create it, then fail gracefully.

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"