Directory Open Issue when passing path as parameter - c++

I having an issue with my program which is expected to either ask a user to input a directory path or to use an predefine directory path as an argument so that my program can open that directory.
Asking a user to input the directory path (C:\Users\Desktop\Test) works fine. But I am having an issue when I am passing the file path as argument.
It should be passed as this:
char location[1000] = "C:\Users\Desktop\Test";
if ((dir = opendir (location)) != NULL){
......
}
But using argument, my program can only open the directory if location is initialised and assigned as this:
char location[1000] = "C:\\Users\\Desktop\\Test";
if ((dir = opendir (location)) != NULL){
......
}
However, I need to concatenate location with a filename in another part of my program so that it becomes: C:\Users\Desktop\Test\file.txt
It won`t work with: C:\\Users\\Desktop\\Test\\file.txt.
char location[1000] can`t be modified as it works well with my code when opening or closing directory.

Related

getenv core dump error in daemon service program

I wrote a daemon service program and I want to open a file in /home/user path while the service is active. But I don't want to open file as root. The first thing I tried for this was to get the user name with the getenv("USER") function, keep it in a string, and then open a txt file with this string path. But after daemonize the program getenv function caused core dump.
int main(){
make_daemon();
const char *userName = "USER";
const char *homePath = "/home/";
const char *env_p = getenv(userName); //Core dump!
std::string m_filePath = homePath;
m_filePath += env_p;
m_filePath += "/test.txt"; // "/home/<user>/test.txt"
//open filePath
//some stuff...
}
I can probably solve this problem by running the service as root and creating a text file somewhere in the /usr/local/ path, but I want to create the text file with the user, not root. Is it possible or is there any other way to do this?

Opening Multiple files from a directory C++

I am trying to open files from a directory but everytime I display my files i have these 3 dots at the top. So for example the directory i open is called "My Documents" the output would be the 3 dots as follows
.
..
Names.txt
Jobs.txt
Names.txt and Jobs.txt is the only output i want to achieve, could anyone help out.
My code
int getDoc(string doc, vector<string> &documents){
DIR *dp;
struct dirent *dirp;
if ((dp = opendir(doc.c_str())) == NULL){
cout << "Error(" << errno << ") opening" << doc << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL){
documents.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
by the way i use dirent.h
. is current directory, .. is upper level directory. if you don't want them, just filter them out.
The 3 dots are actually 2 directories. The first one named '.' refers to the current directory. If you will try to open it, it will lead you back to the same directory. For example, directory C:\Users\Daniel is equal to C:\Users\Daniel\.
The second directory is '..'. It refers to the parent directory. So C:\Users directory is equivalent to C:\Users\Daniel\..
Those 2 directories are not real. They are simulated by the operating system. If you don't want to print them, just start printing the list after skipping the first 2 elements. Those 2 directories are always listed first.
The directory . is the current directory. The directory .. is the parent directory. So they will be on the list
Just like running the command ls -a

Change file/path extension

Using VC++, how can I remove the extension from the following file path and change it to a new extension(using strings):
CString path(_T(m_DirTree.GetCurrentDir())); // copy file path to variable 'path' of type CString
//Add code here....
The path to the file in question is L:\PowerStar 5 Demo II\Programs\Demo\Programs\33100.PRG and I would like to change the file extension to 33100.CRC. Is there some way I could use _splitpath to change the file extension to .CRC? This path is one of many which can be selected via a directory tree that is passed to variable path and I am just using this particular path as an example. So I cannot alter it as below:
CString path(_T("L:\PowerStar 5 Demo II\Programs\Demo\Programs\33100.CRC"));
Is it possible to concatenate the strings so I can open without getting an exception?
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
// Split path to isolate file extension(.prg) for if statement
_splitpath(m_DirTree.GetCurrentDir(), drive, dir, fname, ext);
CString crcFile;
crcFile = strcat(fname,".CRC"); // concatenate chars to point to .CRC file of same name
FILE *cr = fopen(fname, "r"); // Handle to the file in question
The above code throws an unhandled exception.
Try using the Shell API function PathRenameExtension. Or if you want the buffer management handled for you CPathT::RenameExtension, for example:
CPath path(_T("L:\\PowerStar 5 Demo II\\Programs\\Demo\\Programs\\33100.PRG"));
path.RenameExtension(_T(".CRC"));
CString modifiedPath = path;
CString has 2 methods, which might help you.
ReverseFind() and Left()
CString filenameWithoutExtension = path.Left(path.ReverseFind(_T('.')));
Then you can add your new file extension (e.g. ".exe") at the end of the new string.
path = filenameWithoutExtension + _T(".exe");

opendir error?

I get an error with this istruction:
dp = opendir ("%APPDATA%/.");
output:
Couldn't open directory: Mo such file or directory.
but I don't get an erro with this istruction:
dp = opendir ("C:/Users/xrobot/AppData/.");
output:
.
..
Local
LocalLow
Roaming
Why ?
opendir doesn't expand meta variables like %APPDATA%, the shell does. So such things work from the command line, but not from a program. In your program, you have to use an absolute or relative path.
You can probably obtain the required path with getenv(),
const char *appData = getenv("APPDATA");
if (appData) {
dp = opendir(appData);
} else {
/* die or recover */
}
Because the first opendir is LITERALLY trying to open the directory %APPDATA%/..

Get actual folder path

How I can get actual folder path where my program is without my exe file name in C++?
The following function will give you the application path:
::GetModuleFileName(NULL, szAppPath, MAX_PATH);
Now to extract the folder, you need to find the last backslash:
char szApplicationPath[MAX_PATH] = "";
::GetModuleFileName(NULL, szApplicationPath, MAX_PATH);
//Get the folder part
CString strApplicationFolder;
strApplicationFolder = szApplicationPath;
strApplicationFolder = strApplicationFolder.Left(strApplicationFolder.ReverseFind("\\"));