• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

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.
0
Prakashv20
Asked:
Prakashv20
  • 3
  • 3
1 Solution
 
Jase-CoderCommented:
if you are trying to use the variable CFile f in another function, this will fail because f would be out of scope and you would get compiler errors.

What are you expecting to happen because you in the code you have posted it seems your not trying to read from the file.

So I can make some assumptions:

1:You would like to access the variable f in another function.
              To do this you should declare f in your class declaration.

2: You would like to ask f outside of the if statement.
     Declare f at the start of your function rather than inside your if statement. Once the code in the if statement has executed the variable: f will be out of scope and you will not be able to use it.

Also, you should check the return value of CFile::Open for example,

if(f.Open(filename, CFile::modeRead) == TRUE)
  MessageBox("The file was opened");
0
 
Prakashv20Author Commented:
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.
0
 
mahesh1402Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jase-CoderCommented:
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.
0
 
mahesh1402Commented:
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

0
 
Jase-CoderCommented:
>> 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())
0
 
mahesh1402Commented:
@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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now