Get dialog.GetDirectory() with slashes included

13 views
Skip to first unread message

Jason Liam

unread,
Aug 15, 2021, 3:38:06 AM8/15/21
to wx-u...@googlegroups.com
Hi, i have a file dialog and when the user selects a particular file i need to know the directory and the fullpath of the file. For this I am using dialog.GetPath() to know the full path which is what I need. But when I use dialog.GetDirectory() I get the directory of the file but without any "slashes" at the end. That is, I get something like: /home/user/Documents/wxWidgets/myDIR
Note that there is no slash at the end of the above directory. I need the slash at the end because I am passing the directory that I got back from dialog.GetDirectory() to another function that needs a std::string with slash included at the end. For this I convert the directory or filepath from wxString to std::string using the toStdString() method of wxString. Is there a platform independent way to add the appropriate slashes at the end of the directory path. For example if the user is on Windows then the directory path that I might get back would be something like C::\\Documents\\myDir and now in this case at the end there should be 2 backslashes instead of 1 forward slash. I am thinking of using a condition like:
if(dialog.GetDirectory().find("/"))
{
    std::cout<<"got single forward slash"<<std::endl;
   appendedDirPath  = dialog.GetDirectory() + "/";
}
else if(dialog.GetDirectory().find("\\"))
{
    std::cout<<"got double backward slashes"<<std::endl;
   appendedDirPath  = dialog.GetDirectory() + "\\";
}
else if(dialog.GetDirectory().find("\"))
{
    std::cout<<"got single backward slash"<<std::endl;
   appendedDirPath  = dialog.GetDirectory() + "\";
}
Is this the correct way to do/achieve this?
Also, should I use toStdString() or ToUtf8() to convert wxString to std::string for this(above) purpose.

PS: I need the slash at the end because in my function that accepts a std::string, it opens up a particular file in the directory passed as argument. Example let's say the directory that was passed in the function call was /home/user/Documents/
Now using some other library(not wxWidgets), a text file is opened like:
open(passedDirectoryArg + "file.txt"). The argument passedDirectoryArg must have a slash at the end. So if the user passed a directory without a slash at the end then the call to open() would fail.

Jason Liam

unread,
Aug 15, 2021, 7:42:14 AM8/15/21
to wx-u...@googlegroups.com
I used a for loop to check if there are any backslashes or forward slashes in the std::string obtained from dialog.GetDirectory().ToStdString() and added the slash at the end depending on that. This works as expected(on Linux at least) and most probably will work on Windows too(though i haven't checked it on windows yet).

Vadim Zeitlin

unread,
Aug 15, 2021, 8:52:11 AM8/15/21
to wx-u...@googlegroups.com
On Sun, 15 Aug 2021 13:07:52 +0530 Jason Liam wrote:

JL> Hi, i have a file dialog and when the user selects a particular file i need
JL> to know the directory and the fullpath of the file.

Generally speaking, you might want to have a good look at wxFileName class
(or, if you're already using compiler implementing std::filesystem, at it
instead).

JL> Is there a platform independent way to add the appropriate slashes at
JL> the end of the directory path.

Just append wxFileName::GetPathSeparator() to the end of the string.

JL> Also, should I use toStdString() or ToUtf8() to convert wxString to
JL> std::string for this(above) purpose.

This depends on what your other function expects. You need to use UTF-8 if
you want to represent all the paths, including those using non-ASCII
characters, but if your function takes std::string it might not support
them in the first place.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Tim Roberts

unread,
Aug 16, 2021, 12:22:56 PM8/16/21
to wx-users
```
else if(dialog.GetDirectory().find("\"))
{
    std::cout<<"got single backward slash"<<std::endl;
   appendedDirPath  = dialog.GetDirectory() + "\";
}
```
If you haven't typed that in yet, don't; that's a syntax error.  It is important to remember that your path string does not actually contain double backslashes.  You're confusing the PRESENTATION of the string with the CONTENTS of the string.  The debugger is going to display it to you that way to avoid confusion, and you have to use doubled backslashes when typing string constants, but they aren't present in the string.

So, this string:
    char * x = "abc\\\n\\";
takes 11 keystrokes to type, but contains exactly 6 characters: 'a', 'b', 'c', a backslash, a newline, and a backslash.

Typing "\" is a syntax error, because the backslash will escape the double quote, and leave the first double quote unclosed.

Jason Liam

unread,
Aug 16, 2021, 1:31:30 PM8/16/21
to wx-u...@googlegroups.com
@Tim Yes i know. We must have even number of \ then only its correct syntax. Having a single \ or odd number of \ is not correct. Thanks anyways. The example (the 3 if else conditions) that i gave above were written in a hurry so they were not correct. Thanks for pointing it out. For now i am using a for loop and checking for a slash or as suggested by VZ can do the same using wxFileName::GetPathSeperator().

--
Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/dd4f9515-1644-4cdc-829f-91e185049a5fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages