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. How to change current working directory in c/c++?

How to change current working directory in c/c++?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++question
8 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
    Gofur Halmurat
    wrote on last edited by
    #1

    Hello everybody, I am trying to change working directory in c++, For example: My programs is in c://work, I run the program from there, but during running this program, I have to change working directory, do something there.I tried to do it like this:

    system("cd c:/work/test");

    but it does not change working directory Is there a way to change current working directory in c/c++? Thanks in advance

    It is never late to learn

    S CPalliniC 2 Replies Last reply
    0
    • G Gofur Halmurat

      Hello everybody, I am trying to change working directory in c++, For example: My programs is in c://work, I run the program from there, but during running this program, I have to change working directory, do something there.I tried to do it like this:

      system("cd c:/work/test");

      but it does not change working directory Is there a way to change current working directory in c/c++? Thanks in advance

      It is never late to learn

      S Offline
      S Offline
      sashoalm
      wrote on last edited by
      #2

      chdir is the function you should use. as for what you tried it works in the sense that cmd.exe which is started as a child process whenever you call system() changes its working directory.

      There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

      G 1 Reply Last reply
      0
      • S sashoalm

        chdir is the function you should use. as for what you tried it works in the sense that cmd.exe which is started as a child process whenever you call system() changes its working directory.

        There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

        G Offline
        G Offline
        Gofur Halmurat
        wrote on last edited by
        #3

        chdir is c/c++ function? or shell function? can i use it like

        system("chdir c:/work/test");

        It is never late to learn

        CPalliniC 1 Reply Last reply
        0
        • G Gofur Halmurat

          Hello everybody, I am trying to change working directory in c++, For example: My programs is in c://work, I run the program from there, but during running this program, I have to change working directory, do something there.I tried to do it like this:

          system("cd c:/work/test");

          but it does not change working directory Is there a way to change current working directory in c/c++? Thanks in advance

          It is never late to learn

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          _chdir [^]. SetCurrentDirectory [^]. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          G 1 Reply Last reply
          0
          • G Gofur Halmurat

            chdir is c/c++ function? or shell function? can i use it like

            system("chdir c:/work/test");

            It is never late to learn

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Gofur Halmurat wrote:

            can i use it like system("chdir c:/work/test");

            Yes, with the same result you got before. :rolleyes: :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • CPalliniC CPallini

              _chdir [^]. SetCurrentDirectory [^]. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              G Offline
              G Offline
              Gofur Halmurat
              wrote on last edited by
              #6

              Actually, I am using GNU c compiler for UNIX, it seems chdir and SetCurrentDirectory for windows only, is there any other way for UNIX?

              It is never late to learn

              CPalliniC H 2 Replies Last reply
              0
              • G Gofur Halmurat

                Actually, I am using GNU c compiler for UNIX, it seems chdir and SetCurrentDirectory for windows only, is there any other way for UNIX?

                It is never late to learn

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Gofur Halmurat wrote:

                I am using GNU c compiler for UNIX

                GNU isn't for Linux (after all "Gnu is Not Unix" )?

                Gofur Halmurat wrote:

                it seems chdir and SetCurrentDirectory for windows only, is there any other way for UNIX?

                chdir should be available on any POSIX compliant system, like GNU/Linux. Are you aware that CodeProject has a Linux forum?

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • G Gofur Halmurat

                  Actually, I am using GNU c compiler for UNIX, it seems chdir and SetCurrentDirectory for windows only, is there any other way for UNIX?

                  It is never late to learn

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  Did you use if these functions and what was result?

                  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