C++ Directory Functions
-
Is it possible to have a C++ program work only if it is within a certain directory, say.. C:\Program Files\Random Folder ? If so, any tips as to what code I would use?
-
Is it possible to have a C++ program work only if it is within a certain directory, say.. C:\Program Files\Random Folder ? If so, any tips as to what code I would use?
Use
_getcwd()
orGetCurrentDirectory()
and check the results. PS. The comment below to use GetModuleFileName(NULL, ...) is the right answer. You can then compare the string using lstrcmpi(), the Win32 CompareString() function or something similar.Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
modified on Tuesday, April 21, 2009 11:18 AM
-
Use
_getcwd()
orGetCurrentDirectory()
and check the results. PS. The comment below to use GetModuleFileName(NULL, ...) is the right answer. You can then compare the string using lstrcmpi(), the Win32 CompareString() function or something similar.Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
modified on Tuesday, April 21, 2009 11:18 AM
#include // for getcwd #include // for MAX_PATH #include #include // for cout and cin using namespace std; // function to return the current working directory // this is generally the application path void GetCurrentPath(char* buffer) { getcwd(buffer, _MAX_PATH); } int main() { // _MAX_PATH is the maximum length allowed for a path char CurrentPath[_MAX_PATH]; // use the function to get the path GetCurrentPath(CurrentPath); // display the path for demo purposes only char temp[_MAX_PATH]; char temp1[_MAX_PATH]={"C:\\Program Files"}; if(CurrentPath==temp1){ cout << CurrentPath << endl; cout << temp1 << endl;} cout << "Press Enter to continue"; cin.getline(temp,_MAX_PATH); return 0; } Thats what I have, and it outputs the current path and what temp1 holds when the if statement is gone, but no matter what I cannot get the program to check if its in the right directory. Any more tips?
-
#include // for getcwd #include // for MAX_PATH #include #include // for cout and cin using namespace std; // function to return the current working directory // this is generally the application path void GetCurrentPath(char* buffer) { getcwd(buffer, _MAX_PATH); } int main() { // _MAX_PATH is the maximum length allowed for a path char CurrentPath[_MAX_PATH]; // use the function to get the path GetCurrentPath(CurrentPath); // display the path for demo purposes only char temp[_MAX_PATH]; char temp1[_MAX_PATH]={"C:\\Program Files"}; if(CurrentPath==temp1){ cout << CurrentPath << endl; cout << temp1 << endl;} cout << "Press Enter to continue"; cin.getline(temp,_MAX_PATH); return 0; } Thats what I have, and it outputs the current path and what temp1 holds when the if statement is gone, but no matter what I cannot get the program to check if its in the right directory. Any more tips?
gamefreak2291 wrote:
if(CurrentPath==temp1){
You should be using some sort of string compare routine here (e.g.,
strcmp()
). Have you used the debugger to step through the code?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
gamefreak2291 wrote:
if(CurrentPath==temp1){
You should be using some sort of string compare routine here (e.g.,
strcmp()
). Have you used the debugger to step through the code?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Thanks again, opened up my book, went right to str functions found strcmp and here is the working code:
// The Code is Borland's, I just modified it
// to make it Standard C++#include // for getcwd
#include // for MAX_PATH
#include
#include // for cout and cinusing namespace std;
// function to return the current working directory
// this is generally the application path
void GetCurrentPath(char* buffer)
{
getcwd(buffer, _MAX_PATH);
}int main()
{// _MAX_PATH is the maximum length allowed for a path
char CurrentPath[_MAX_PATH];
// use the function to get the path
GetCurrentPath(CurrentPath);// display the path for demo purposes only
char temp[_MAX_PATH];
char temp1[_MAX_PATH]={"C:\\Program Files\\uTorrent"};
if(strcmp(CurrentPath, temp1)==0){
cout << CurrentPath << endl;cout << temp1 << endl;}
cout << "Press Enter to continue";
cin.getline(temp,_MAX_PATH);
return 0;
} -
Is it possible to have a C++ program work only if it is within a certain directory, say.. C:\Program Files\Random Folder ? If so, any tips as to what code I would use?
Use GetModuleFileName(). The other response is not going to work in all cases (GetCurrentDirectory) since your program could be run from a shortcut on the desktop, and that shortcut could specify a different working directory.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193