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

Help with CFile

As I am new in C++ I would like to write the folowing code using the CFile class
while(!feof(fp)){
   do something
  }
Also is there an equivalent to
if(ferror(fp))
Thanks
0
vardis
Asked:
vardis
  • 3
  • 3
1 Solution
 
vardisAuthor Commented:
Edited text of question
0
 
danny_pavCommented:
1.  You can use the member m_hFile which is a file handle and do all the work with that.

-- or --

2.  You can read until end of file
char buf[100];
UINT u_bytes_read = pFile->Read(buf, sizeof(buf));
if (u_bytes_read != sizeof(buf)) {
//end of file encountered
}

Use GetStatus to check for errors

0
 
vardisAuthor Commented:
thank you for your reply
Can you give me an example of how to use the member m_hFile
thanks
0
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!

 
danny_pavCommented:
you use the functions ReadFile and WriteFile on the handle, BUT
you would probably want to use the CFile::ReadFile and CFile::WriteFile which are generally wrappers for these functions but will maintain the state of the object as well.  Using the m_hFile is a way around the class when in fact what you want to do is probably supported already.

0
 
danny_pavCommented:
also, as far as ferror is concerned, you can use GetLastError(); and the CFile class will throw exceptions, so you can use a try/catch block.
0
 
vardisAuthor Commented:
Thank you danny_pav this answers my question.

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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