How to get filename and Dir name at the same time? Topic is solved

This forum is reserved for everything you want to talk about. It could be about programming, opinions, open source programs, development in general, or just cool stuff to share!
Post Reply
In need of some credit
In need of some credit
Posts: 3
Joined: Sat Jul 27, 2013 8:27 pm

How to get filename and Dir name at the same time?

Post by mekanik » Mon Jul 29, 2013 2:20 am

Hi.. I have been looking around a way to get the File name and Dir name with a same dialog, in this case wxFileDialog. But hitting roadblocks all the way..
Below is not a elegant code but i was trying to get a hang of wxWidgets..
I tried using BeforeLast to get the Dir name but not sure how to make it a generic code ,i.e., make it work with all kinds of file name..
But then i am not sure how to get the file name alone..

Ok the aim is that i will get a input as a file but i need to separate the file name and dir name..

Code: Select all

void Test2Frame::OnOpen(wxCommandEvent& WXUNUSED(event))
    wxString file, fileDir, tempName;
    wxFileDialog* openFileDialog = new wxFileDialog(this, _("Open XML file"), "", "","XML files| *.xml", wxFD_OPEN|wxFD_FILE_MUST_EXIST);

    if (openFileDialog->ShowModal() == wxID_CANCEL)

    file = openFileDialog->GetPath();
        wxLogMessage("File output %s", file);

    wxFileInputStream input_stream(file);
    if (!input_stream.IsOk())
        wxLogError("Cannot open file '%s'.", file);
    //File size
    struct stat fileStat; stat(file, &fileStat);
    wxCharBuffer filepath = file.ToUTF8();
  //do something with the file.. 

    size_t pathLen = file.Len();
        wxLogMessage("File length %d", pathLen);

    wxLogMessage("BeforeLast %s", file.BeforeLast('a'));// get the Dir name but the file name needs to start with "a"
Suggetsions please..

User avatar
Posts: 14874
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to get filename and Dir name at the same time?

Post by doublemax » Mon Jul 29, 2013 6:22 am

Code: Select all

#include <wx/filename.h>

wxFileName fname( openFileDialog->GetPath() );

wxLogMessage( wxT("filename=%s"), fname.GetFullName() );
wxLogMessage( wxT("path=%s"), fname.GetPath() );
wxLogMessage( wxT("path with separator=%s"), fname.GetPathWithSep() );
Use the source, Luke!

Post Reply