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 write output from a c++ program to a subdirectory of a directory where the .exe resides.

how to write output from a c++ program to a subdirectory of a directory where the .exe resides.

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
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.
  • M Offline
    M Offline
    mrby123
    wrote on last edited by
    #1

    Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks

    M D _ 3 Replies Last reply
    0
    • M mrby123

      Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      your "code" does not make sense. What's the question ? - You know how to create files/directories and write to them ? - You know where the executable resides ? - You know how to check if you have permission to write there ? As far as I know, it's bad practice to write files where the exe resides, more than often a user will not be able to do so, use the user's "document" folders. Good luck. Max.

      Watched code never compiles.

      1 Reply Last reply
      0
      • M mrby123

        Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks

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

        mrby123 wrote:

        f_ptr22[i] = fopen("rs_sf"+ftitle,"w");

        If you really have to, try:

        f_ptr22[i] = fopen(".\\subdir\\rs_sf" + ftitle, "w");

        "One man's wage rise is another man's price increase." - Harold Wilson

        "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

        "Man who follows car will be exhausted." - Confucius

        M 1 Reply Last reply
        0
        • M mrby123

          Hi I would like to tidy up the directories. So I like to my program to read input from a sub-directory and write output to a sub-directory. All these two sub-directories are under the directory where the .exe resides. FILE *f_ptr1; CString ftitle; f_ptr22[i] = fopen("rs_sf"+ftitle,"w"); fprintf (f_ptr2, "%.6lf\n",v[2*i]); Thanks

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          As Maximilien said, programs are normally installed under the C:\Program Files directory. You need administrative privileges to create folders and files under this directory. You can get the Documents folder using the SHGetSpecialFolderLocation API and specifying CSIDL_MYDOCUMENTS in its nFolder parameter.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          1 Reply Last reply
          0
          • D David Crow

            mrby123 wrote:

            f_ptr22[i] = fopen("rs_sf"+ftitle,"w");

            If you really have to, try:

            f_ptr22[i] = fopen(".\\subdir\\rs_sf" + ftitle, "w");

            "One man's wage rise is another man's price increase." - Harold Wilson

            "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

            "Man who follows car will be exhausted." - Confucius

            M Offline
            M Offline
            mrby123
            wrote on last edited by
            #5

            Thanks, I tried. It does not work. I need: if the sub directory does not exist,create it and write the file in it, if it exists, write the file in it without re-create it. Thanks for further suggestions.

            D 1 Reply Last reply
            0
            • M mrby123

              Thanks, I tried. It does not work. I need: if the sub directory does not exist,create it and write the file in it, if it exists, write the file in it without re-create it. Thanks for further suggestions.

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

              Well, what I showed would obviously require that the "subdir" folder exist. Use CreateDirectory() first.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "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

              "Man who follows car will be exhausted." - Confucius

              M 1 Reply Last reply
              0
              • D David Crow

                Well, what I showed would obviously require that the "subdir" folder exist. Use CreateDirectory() first.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "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

                "Man who follows car will be exhausted." - Confucius

                M Offline
                M Offline
                mrby123
                wrote on last edited by
                #7

                Thanks. I think I also need a test statement to check if the directory exists. If it does not exist, then create the directory. Otherwise, just write the file in the directory. What is the statement for checking if a directory exist ? Thanks again

                D 1 Reply Last reply
                0
                • M mrby123

                  Thanks. I think I also need a test statement to check if the directory exists. If it does not exist, then create the directory. Otherwise, just write the file in the directory. What is the statement for checking if a directory exist ? Thanks again

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

                  mrby123 wrote:

                  What is the statement for checking if a directory exist ?

                  You could either call _access() or let CreateDirectory() do it for you.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "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

                  "Man who follows car will be exhausted." - Confucius

                  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