Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

file browser in vc++

Posted on 2003-02-25
8
Medium Priority
?
391 Views
Last Modified: 2007-12-19
how to add file browser button(code) on GUI,that will allow me to select a particular file in a particular directory.
0
Comment
Question by:kaushik787
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 80 total points
ID: 8016640
If you're using MFC, you can use the CFileDialog class to help find the file.  Like:

CFileDialog fd(TRUE);
fd.m_ofn.lpstrFilter = "Text files\0*.txt\0All Files\0*.*\0"; // Use NULLS to separate the filters
// from the text to be displayed
if(fd.DoModal() == IDOK) // Display the dialog
{
  CString file_name = fd.GetPathName();
  MessageBox(file_name);
}
0
 

Author Comment

by:kaushik787
ID: 8022832
i need a detailed description, i'm designing a GUI for first time.Is there any built in control to do this job.
0
 

Author Comment

by:kaushik787
ID: 8023005
hi mnashadka it's helping,the only problem now is that how do i give the path in edit box which is suppose to show file,instead of showing it on message box.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:anjalidevi
ID: 8023952
Hi kaushik787 ,

Try this


-Anjali

#include <windows.h>
#include <commdlg.h>

GetFileName( char* sFName);

main()
{
char sFName[100];
/* the "sFname "variable will have the name of the file selected
*/

/*Call the function*/
GetFileName(sFName);
/*use sFname to get the selected file*/

}


bool GetFileName( char* sFName)
{

     TCHAR szFilename[MAX_PATH] = TEXT("");

     TCHAR FlExtn[MAX_PATH] = TEXT("all Files");

    BOOL bResult = FALSE;
    DWORD dwError = NOERROR;
    OPENFILENAME ofn = {0};

    ofn.lStructSize          = sizeof (OPENFILENAME);
     ofn.lpstrFilter          = TEXT("all Files(*.*)\0*.*\0\0");
    ofn.lpstrFile          = szFilename;
    ofn.nMaxFile          = MAX_PATH;
     ofn.lpstrInitialDir = NULL;
     ofn.lpstrDefExt          = FlExtn;
     
     ofn.Flags = OFN_EXPLORER |
                    OFN_ENABLEHOOK |
                    OFN_FILEMUSTEXIST|
                    OFN_EXTENSIONDIFFERENT|
                    OFN_PATHMUSTEXIST;
     ofn.lpstrTitle = "Open";
     bResult = GetOpenFileName(&ofn);
     


    if (bResult == FALSE)
    {
        dwError = CommDlgExtendedError ();
        return false;
    }
     
          strcpy(sFName,szFilename);
          return true;
}

0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8024219
If you have an edit box, the easiest way to show the path is to assign a variable to the edit box.  If you have a CString named m_filename that is assigned to the edit box (using the ClassWizard or Add Variable in the ClassView if you're using the .NET version of the compiler), it might look like:

CFileDialog fd(TRUE);
fd.m_ofn.lpstrFilter = "Text files\0*.txt\0All Files\0*.*\0"; // Use NULLS to separate the filters
// from the text to be displayed
if(fd.DoModal() == IDOK) // Display the dialog
{
 m_filename = fd.GetPathName(); // Assign the variable to the result
 UpdateData(FALSE); // Update the data in the windows
}
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8024238
If it's in a dialog box, you could also use SetWindowText.  If the control id of the edit box was IDC_FILENAME, it would be:
CFileDialog fd(TRUE);
fd.m_ofn.lpstrFilter = "Text files\0*.txt\0All Files\0*.*\0"; // Use NULLS to separate the filters
// from the text to be displayed
if(fd.DoModal() == IDOK) // Display the dialog
{
  GetDlgItem(IDC_FILENAME)->SetWindowText(fd.GetPathName());
}
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8024341
If you make the edit box multiline, then you can read the file and do a SetWindowText or update a variable with the contents using ifstream or CFile.  Would you rather be more Microsoft-specific or standard?  My e-mail is mike.nashadka@sanmina-sci.com
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9845985
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to mnashadka: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question