Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Directory Functions

C++ Directory Functions

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionlounge
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gamefreak2291
    wrote on last edited by
    #1

    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?

    J K 2 Replies Last reply
    0
    • G gamefreak2291

      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?

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      Use _getcwd() or GetCurrentDirectory() 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

      G 1 Reply Last reply
      0
      • J Joe Woodbury

        Use _getcwd() or GetCurrentDirectory() 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

        G Offline
        G Offline
        gamefreak2291
        wrote on last edited by
        #3

        #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?

        D 1 Reply Last reply
        0
        • G gamefreak2291

          #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?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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

          G 1 Reply Last reply
          0
          • D David Crow

            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

            G Offline
            G Offline
            gamefreak2291
            wrote on last edited by
            #5

            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 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\\uTorrent"};
            if(strcmp(CurrentPath, temp1)==0){
            cout << CurrentPath << endl;

            cout << temp1 << endl;}
            cout << "Press Enter to continue";
            cin.getline(temp,_MAX_PATH);
            return 0;
            }

            1 Reply Last reply
            0
            • G gamefreak2291

              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?

              K Offline
              K Offline
              krmed
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups