•
•
•
•
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
![]() |
•
•
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation:
Rep Power: 2
Solved Threads: 2
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,
My question is there is any simple way to do it. I know on fstream I can do it within one statement, like this.
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,
C++ Syntax (Toggle Plain Text)
int length = 0 ; CString str length = srf_ReadFile.GetLength()/sizeof(TCHAR) ; str.Format(_T("File size is %d bytes long."), length) ; 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.
C++ Syntax (Toggle Plain Text)
while(!fileopen.eof())
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
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).
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.
•
•
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation:
Rep Power: 2
Solved Threads: 2
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.
•
•
•
•
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.
•
•
•
•
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
>>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.
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.
•
•
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation:
Rep Power: 2
Solved Threads: 2
Thanks pal,
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.
•
•
•
•
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- What is EOF in stdin? (C)
- C Program where gets(), getchar(), are not working. (C)
- how do you loop fscanf until EOF in c? (C)
- how do you loop fscanf until EOF in c? (C)
- How works EOF (C)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: Linked List Problem
- Next Thread: help drawing triangles



Linear Mode