User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,582 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1682 | Replies: 6 | Solved
Reply
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Solution EOF in a CFile

  #1  
Oct 31st, 2007
Hi all,

I want to find the end of file in a CFile. What I've do up to now is, find the length of the file in bytes. Then do the required process with the number of bytes. Here is the code,

  1. int length = 0 ;
  2. CString str
  3.  
  4. length = srf_ReadFile.GetLength()/sizeof(TCHAR) ;
  5. str.Format(_T("File size is %d bytes long."), length) ;
  6. AfxMessageBox(str, MB_OK) ;


My question is there is any simple way to do it. I know on fstream I can do it within one statement, like this.

  1. while(!fileopen.eof())
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: EOF in a CFile

  #2  
Oct 31st, 2007
even the fstream example you posted is not workable.

line 4 does not calculate the size of a file but the number of TCHARs in the file, which might or might not be the same as the size depending on the UNICODE setting.

As for CFile, the answer is NO -- and that's one reason why I hate that class. Unless the file contains serialized MFC objects you are better off using normal c++ fstreams, assuming your compiler supports it (some do not).
Last edited by Ancient Dragon : Oct 31st, 2007 at 4:49 am.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: EOF in a CFile

  #3  
Oct 31st, 2007
Originally Posted by Ancient Dragon View Post
even the fstream example you posted is not workable.


At the time I post my code it's not workable. But now it works. What I want to do is try the same application with CFile and SQL Server. And also I want to design a GUI for it later.

Originally Posted by Ancient Dragon View Post
line 4 does not calculate the size of a file but the number of TCHARs in the file, which might or might not be the same as the size depending on the UNICODE setting.

Actually it works, I check the properties of the file(file which is used to read) and the number of bytes of the file is same as the value given by the above code.

Originally Posted by Ancient Dragon View Post
As for CFile, the answer is NO -- and that's one reason why I hate that class. Unless the file contains serialized MFC objects you are better off using normal c++ fstreams, assuming your compiler supports it (some do not).


Most of the time I found that, people don't like MFC. Few reasons I have to use it. First thing is I want to design GUI. Second thing is I'm really new for C++ and MFC, and I'm searching the easiest way to do it.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: EOF in a CFile

  #4  
Oct 31st, 2007
>>Actually it works
Yes it will work if you do not define UNICODE because TCHAR is defined as char and sizeof(char) is always 1. So your code reduces to srf_ReadFile.GetLength()/1, or just simply srf_ReadFile.GetLength(). Now define UNICODE and your formula will change to srf_ReadFile.GetLength()/2, which will result in 1/2 of the actual file size if you are on MS-Windows or 1/4th the file size on *nix because sizeof(TCHAR) = 2 (a short int) on MS-Windows and sizeof(TCHAR) is 4 on *nix (a long int)

>>Few reasons I have to use it. First thing is I want to design GUI
Not a reason to use CFile. Just because you have an MFC program is not a reason to use CFile either. You can mix CFile with fstreams in the same program -- I have done it hundreds of times.
Last edited by Ancient Dragon : Oct 31st, 2007 at 8:58 am.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: EOF in a CFile

  #5  
Nov 4th, 2007
Thanks pal,

Originally Posted by Ancient Dragon View Post
Not a reason to use CFile. Just because you have an MFC program is not a reason to use CFile either. You can mix CFile with fstreams in the same program -- I have done it hundreds of times.


Actually I haven't use it before. If I can use fstream in an MFC application what is the point to use CFile. Any special reason to do it.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: EOF in a CFile

  #6  
Nov 5th, 2007
CFile is often used with CArchive to serialize MFC classes and objects. Otherwise its not a very good class for normal text file i/o.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: EOF in a CFile

  #7  
Nov 5th, 2007
Actually lots of people hate this MFC. But I feel it is easy to learn.

And as you say, if I can use functionalities like fstream, etc it wont be difficult at all.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC