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. Creating an escaped windows path?

Creating an escaped windows path?

Scheduled Pinned Locked Moved C / C++ / MFC
csharptutorialquestion
7 Posts 2 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.
  • H Offline
    H Offline
    hyling
    wrote on last edited by
    #1

    Is there an easily way to create an escaped Window's path, or is there a better way to do this? I have code that gets a path from a CFileDialog then I want to open the file with fopen because I want the code to be portable. example: CFileDialog fileDlg; CString fileName = fileDlg.GetPathName(); FILE *file = fopen(fileName, "r"); I've done google searches but found nothing helpful, unless I'm using C# in which case they have @"" strings that handle this nicely. Thanks Hua-Ying

    D 1 Reply Last reply
    0
    • H hyling

      Is there an easily way to create an escaped Window's path, or is there a better way to do this? I have code that gets a path from a CFileDialog then I want to open the file with fopen because I want the code to be portable. example: CFileDialog fileDlg; CString fileName = fileDlg.GetPathName(); FILE *file = fopen(fileName, "r"); I've done google searches but found nothing helpful, unless I'm using C# in which case they have @"" strings that handle this nicely. Thanks Hua-Ying

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

      What you have is fine, except that fileDlg.DoModal() needs to be called. BTW, why are you using a FILE object rather than a CFile object?


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      H 1 Reply Last reply
      0
      • D David Crow

        What you have is fine, except that fileDlg.DoModal() needs to be called. BTW, why are you using a FILE object rather than a CFile object?


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        H Offline
        H Offline
        hyling
        wrote on last edited by
        #3

        Thanks for pointing that out. I am calling fileDlg.DoModal() in the real code, I didn't put it in the sample code for simplicity. I'm using FILE instead of CFile for portability. That code needs to run under other platforms such as Linux. The problem is fileDlg returns a string in this format: "c:\testfile.txt" but fopen wants this: "c:\\testfile.txt".

        D 1 Reply Last reply
        0
        • H hyling

          Thanks for pointing that out. I am calling fileDlg.DoModal() in the real code, I didn't put it in the sample code for simplicity. I'm using FILE instead of CFile for portability. That code needs to run under other platforms such as Linux. The problem is fileDlg returns a string in this format: "c:\testfile.txt" but fopen wants this: "c:\\testfile.txt".

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

          hyling wrote: That code needs to run under other platforms such as Linux So does the Linux platform support CFileDialog? hyling wrote: The problem is fileDlg returns a string in this format: "c:\testfile.txt" but fopen wants this: "c:\\testfile.txt". Only if you are using a string literal are double backslashes required. When the compiler encounters a backslash, it treats the next character as special (e.g., \n, \t, \a). Since the backslash character is special in and of itself, it must be preceded by a second backslash.


          "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

          H 1 Reply Last reply
          0
          • D David Crow

            hyling wrote: That code needs to run under other platforms such as Linux So does the Linux platform support CFileDialog? hyling wrote: The problem is fileDlg returns a string in this format: "c:\testfile.txt" but fopen wants this: "c:\\testfile.txt". Only if you are using a string literal are double backslashes required. When the compiler encounters a backslash, it treats the next character as special (e.g., \n, \t, \a). Since the backslash character is special in and of itself, it must be preceded by a second backslash.


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            H Offline
            H Offline
            hyling
            wrote on last edited by
            #5

            CFileDialog will be replace with the Linux equivalent for the port. fopen seems to only accept an escaped string even if it is not a string literal, ie. when CFileDialog returns an unescaped string fopen can not open the file. I understand how an escaped string works. I should have been more explicit. I'm asking, is there an easier way to get an escaped version of the string without having to write my own code to escape the '/' in the path. I don't have a problem with writing that code, but I don't want to reinvent the wheel. Thanks Hua-Ying

            D 1 Reply Last reply
            0
            • H hyling

              CFileDialog will be replace with the Linux equivalent for the port. fopen seems to only accept an escaped string even if it is not a string literal, ie. when CFileDialog returns an unescaped string fopen can not open the file. I understand how an escaped string works. I should have been more explicit. I'm asking, is there an easier way to get an escaped version of the string without having to write my own code to escape the '/' in the path. I don't have a problem with writing that code, but I don't want to reinvent the wheel. Thanks Hua-Ying

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

              hyling wrote: I'm asking, is there an easier way to get an escaped version of the string without having to write my own code to escape the '/' in the path. I don't have a problem with writing that code, but I don't want to reinvent the wheel. I do not know of a function, other than writing your own to replace each \ with \\, because up until today, I did not know it was necessary. Have you tried this same experiment on a Windows box?


              "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

              H 1 Reply Last reply
              0
              • D David Crow

                hyling wrote: I'm asking, is there an easier way to get an escaped version of the string without having to write my own code to escape the '/' in the path. I don't have a problem with writing that code, but I don't want to reinvent the wheel. I do not know of a function, other than writing your own to replace each \ with \\, because up until today, I did not know it was necessary. Have you tried this same experiment on a Windows box?


                "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

                H Offline
                H Offline
                hyling
                wrote on last edited by
                #7

                You're right, escaping the string isn't necessary unless it is a string literal. I was confused by the VS.net debugger, which puts a summary of the elements of the FILE structure beside the pointer, the first element of the FILE * happened to be "0(Bad Ptr)", but the FILE * itself was good. Err.. . maybe I just need more caffeine. :zzz: Now I know why nobody else on the net or discussion boards had problem... Thanks for your time. :-O Hua-Ying

                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