Solved

CFILE

Posted on 2006-07-12
7
553 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
  • 3
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 11

Expert Comment

by:Jase-Coder
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
@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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 113
not able to insert into temp table 68 149
matchUp  challenge 6 48
MaxSpan challenge 9 65
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now