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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. create file directly

create file directly

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
6 Posts 3 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.
  • J Offline
    J Offline
    josip cagalj
    wrote on last edited by
    #1

    In my app I need to write something in some place on PC. In my code I call 'std::ofstream' but first I have to make sure that file exist. If file doesn't exist I need to make an empty file. Now my question is, after I find out that file doesn't exist can I create it only by specifying full path-name not worrying if some of sub folders exist. It needs to recreate the whole path. To be more specific the location is 'C:\....\Aplication Data\..". How to achieve this? Thanks

    _ S 2 Replies Last reply
    0
    • J josip cagalj

      In my app I need to write something in some place on PC. In my code I call 'std::ofstream' but first I have to make sure that file exist. If file doesn't exist I need to make an empty file. Now my question is, after I find out that file doesn't exist can I create it only by specifying full path-name not worrying if some of sub folders exist. It needs to recreate the whole path. To be more specific the location is 'C:\....\Aplication Data\..". How to achieve this? Thanks

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

      Use SHCreateDirectory to create the entire path before creating the file.

      «_Superman_»

      1 Reply Last reply
      0
      • J josip cagalj

        In my app I need to write something in some place on PC. In my code I call 'std::ofstream' but first I have to make sure that file exist. If file doesn't exist I need to make an empty file. Now my question is, after I find out that file doesn't exist can I create it only by specifying full path-name not worrying if some of sub folders exist. It needs to recreate the whole path. To be more specific the location is 'C:\....\Aplication Data\..". How to achieve this? Thanks

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        josip cagalj wrote:

        Now my question is, after I find out that file doesn't exist can I create it only by specifying full path-name not worrying if some of sub folders exist. It needs to recreate the whole path.

        You'll have to do that separately from the file create. And even the functions you could use to create a directory (_mkdir or CreateDirectory) will only create a directory if its parent exists. So you're going to have to write something that recurses through a path, creating directories that don't exist in teh appropriate order.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        J _ 2 Replies Last reply
        0
        • S Stuart Dootson

          josip cagalj wrote:

          Now my question is, after I find out that file doesn't exist can I create it only by specifying full path-name not worrying if some of sub folders exist. It needs to recreate the whole path.

          You'll have to do that separately from the file create. And even the functions you could use to create a directory (_mkdir or CreateDirectory) will only create a directory if its parent exists. So you're going to have to write something that recurses through a path, creating directories that don't exist in teh appropriate order.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          J Offline
          J Offline
          josip cagalj
          wrote on last edited by
          #4

          Thanks to all of you!

          1 Reply Last reply
          0
          • S Stuart Dootson

            josip cagalj wrote:

            Now my question is, after I find out that file doesn't exist can I create it only by specifying full path-name not worrying if some of sub folders exist. It needs to recreate the whole path.

            You'll have to do that separately from the file create. And even the functions you could use to create a directory (_mkdir or CreateDirectory) will only create a directory if its parent exists. So you're going to have to write something that recurses through a path, creating directories that don't exist in teh appropriate order.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

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

            Stuart Dootson wrote:

            And even the functions you could use to create a directory (_mkdir or CreateDirectory) will only create a directory if its parent exists.

            SHCreateDirectory will create the entire path for you.

            «_Superman_»

            S 1 Reply Last reply
            0
            • _ _Superman_

              Stuart Dootson wrote:

              And even the functions you could use to create a directory (_mkdir or CreateDirectory) will only create a directory if its parent exists.

              SHCreateDirectory will create the entire path for you.

              «_Superman_»

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              Thanks - I found that just as I'd pressed the 'Post Message' button and was going to amend my post - but then found you'd beaten me to it! I was looking in the shlwapi[^] docs for it. Silly me :-)

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              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