opendir error? - c++

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%/..

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?

c++ How to check if there is a file in a directory quick way?

What I am trying to do is that when a directory has no write permission and if directory contains at least one file trying to print out "permission denied"
DIR *dir;
dir = opendir (argv[i]);
if (!(sb.st_mode & S_IWUSR) && (readdir(dir) != NULL))
{
printf("rm: cannot remove ");
printf(argv[i]);
printf(": Permission denied\n");
}
This is what I was trying to do, but it prints out the messages even when there is no files... Any suggestions?

Directory Open Issue when passing path as parameter

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.

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

Qfile open returns false

I have kept the Content.xml in the same folder where my app.exe exists.
But open call returns false ?
What should be the exact path for the file ?
I do not want to give the complete path for the file but want my code to be path independent "means file which i want to read should be in same folder where my exe is lying"
#define FILE_NAME "Content.xml"
QString xmlFileName(FILE_NAME);
xmlFile.setFileName(xmlFileName);
if ( ! xmlFile.open(QIODevice::ReadOnly|QIODevice::Text) )
{
QMessageBox* msgBox = new QMessageBox();
msgBox->setText("File Not Found !!");
msgBox->setWindowFlags(Qt::WindowStaysOnTopHint);
msgBox->exec();
}
Try to open file with full path like:
xmlFile.setFileName(QCoreApplication::applicationDirPath() + QLatin1Char('/') + xmlFileName);
If you run your code with the run button from Qt Creator, the Content.xml needs to be in the same folder as your code is.