Link to home
Start Free TrialLog in
Avatar of Prakashv20
Prakashv20

asked on

CFILE

i am trying to open a file in wince using EVC++

void CFinalVersionDlg::OnBbrowse()
{
      static CString szFilter1(L"All Files (*.*)|*.*||");    
     CFileDialog cOpenDlg(TRUE,L"*.*",L"*.*",OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,szFilter1);
     
       if( cOpenDlg.DoModal ()==IDOK )
  {
    CString pathName = cOpenDlg.GetPathName();
      MessageBox(pathName,MB_OK);
      CFile f;
      f.Open(pathName,CFile::modeRead);
       }
}

but nothing is happening,I am able to browse the files and then i can see the message box with filepath, but it does not open the file. can anyone please help me on this.
ASKER CERTIFIED SOLUTION
Avatar of Jase-Coder
Jase-Coder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Prakashv20
Prakashv20

ASKER

I am trying to open a file whose path is stored in the variable "pathName". And i am not sure if this is the correct way. I m trying to use the "pathName" to point ot the file.
Prakashv20 ,

what do you get if you try like :

void CFinalVersionDlg::OnBbrowse()
{
       CFile f; // declare CFile object here

     static CString szFilter1(L"All Files (*.*)|*.*||");    
     CFileDialog cOpenDlg(TRUE,L"*.*",L"*.*",OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,szFilter1);
     
      if( cOpenDlg.DoModal ()==IDOK )
     {
      CString pathName = cOpenDlg.GetPathName();
      MessageBox(pathName,MB_OK);
   
        if(f.Open(pathName,CFile::modeRead))  // Open file
        {
          //Do Processing...

         f.Close();   //Close file
        }
     }
}

-MAHESH
dont pick upbad programming habbits replace the if mahesh showed you with the if I posted early.

if(f.Open(filename, CFile::modeRead) == TRUE)

what your doing is correct. Once you have opened the file you can call the read method.
there is no harm in if(f.Open()) as Return Value of MFC CFile class is Nonzero if the open was successful; otherwise 0.  however thats not goin major change here..try to get logic behind it....just check moving CFile object declaration globally or out of if scope and try to open and read file or anyother file processing..

-MAHESH

>> there is no harm in if(f.Open()) as Return Value of MFC

the harm is it leads to bad habbits. For example I think TRUE in the COM world is 0 and FALSE is -1. My point is if you keep doing thinks like: if(f.Open()) you try them with other libraries and you get some little bugs. Also using the TRUE helps with readability.

There is no benefit what so ever to do f.open().

Also make your code readable and use the defined constants because, although it is unlikely in MFC lib, the values of these constants could change and you program would fail if TRUE was set to 0. and your condition statement read if(f.Open())
@Jase-Coder
 what you are saying is useful ..but my aim is here to point towards major change to solve author problem....n in your first comment only you made clear what to do still it seems author is confused so I just shown in source code format example....and also because in your example you shown like :

if(f.Open(filename, CFile::modeRead) == TRUE) <== Dont confuse Here with filename....as you said <replace the if mahesh showed you with the if I posted early> In above code no 'filename' is there and You required 'filename with full pathName' so path Name returned by CFileDialog::GetPathName() is required as shown 'f.Open(pathName,CFile::modeRead). So from authors comment it seems cofused with 'filename' and 'pathname' so I suggested like above..

-MAHESH