?
Solved

CFILE

Posted on 2006-07-12
7
Medium Priority
?
601 Views
Last Modified: 2013-11-20
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
Comment
Question by:Prakashv20
[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
  • 3
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 375 total points
ID: 17088528
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
 

Author Comment

by:Prakashv20
ID: 17094342
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17097044
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
Technology Partners: 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!

 
LVL 11

Expert Comment

by:Jase-Coder
ID: 17097528
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17097588
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
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 17097991
>> 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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17098133
@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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 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