Creating an escaped windows path?
-
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
-
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
What you have is fine, except that
fileDlg.DoModal()
needs to be called. BTW, why are you using aFILE
object rather than aCFile
object?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
What you have is fine, except that
fileDlg.DoModal()
needs to be called. BTW, why are you using aFILE
object rather than aCFile
object?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
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".
-
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".
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
-
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
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
-
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
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
-
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
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