File size in standard way
-
Hi all, One of my application I used MFC(actually CFile) to find the file size in easy way. Now I thought to do it using standard C++. Here is my try.
size\_t file\_size; ifstream in\_file; in\_file.open("C:\\\\temo\_file.txt", ios\_base::in); if(in\_file.is\_open()) { file\_size = in\_file.seekg(-1, ios\_base::end).tellg(); cout << file\_size; }
My question is, this code gives misses one byte. Can you guys give me any reason for it. Thanks
I appreciate your help all the time... CodingLover :)
-
Hi all, One of my application I used MFC(actually CFile) to find the file size in easy way. Now I thought to do it using standard C++. Here is my try.
size\_t file\_size; ifstream in\_file; in\_file.open("C:\\\\temo\_file.txt", ios\_base::in); if(in\_file.is\_open()) { file\_size = in\_file.seekg(-1, ios\_base::end).tellg(); cout << file\_size; }
My question is, this code gives misses one byte. Can you guys give me any reason for it. Thanks
I appreciate your help all the time... CodingLover :)
I guess with seekg you are looking for the position -1 from the end. Therefore you are missing the byte. Additionally look here: http://www.codeproject.com/KB/files/filesize.aspx[^] http://www.cplusplus.com/reference/iostream/istream/seekg.html[^]
-
Hi all, One of my application I used MFC(actually CFile) to find the file size in easy way. Now I thought to do it using standard C++. Here is my try.
size\_t file\_size; ifstream in\_file; in\_file.open("C:\\\\temo\_file.txt", ios\_base::in); if(in\_file.is\_open()) { file\_size = in\_file.seekg(-1, ios\_base::end).tellg(); cout << file\_size; }
My question is, this code gives misses one byte. Can you guys give me any reason for it. Thanks
I appreciate your help all the time... CodingLover :)
CodingLover wrote:
file_size = in_file.seekg(-1, ios_base::end).tellg();
should be
file_size = in_file.seekg(0, ios_base::end).tellg();
BTW you may also use GetFileSize [^]. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
CodingLover wrote:
file_size = in_file.seekg(-1, ios_base::end).tellg();
should be
file_size = in_file.seekg(0, ios_base::end).tellg();
BTW you may also use GetFileSize [^]. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThanks, so my attempt is not bad. :) Is that GetFileName is standard C++. I'm confusing with those.
I appreciate your help all the time... CodingLover :)
-
I guess with seekg you are looking for the position -1 from the end. Therefore you are missing the byte. Additionally look here: http://www.codeproject.com/KB/files/filesize.aspx[^] http://www.cplusplus.com/reference/iostream/istream/seekg.html[^]
Thanks for links.
I appreciate your help all the time... CodingLover :)
-
Thanks, so my attempt is not bad. :) Is that GetFileName is standard C++. I'm confusing with those.
I appreciate your help all the time... CodingLover :)
CodingLover wrote:
Is that GetFileName is standard C++. I'm confusing with those.
Nope. It is provided by
Win32 API
. If you just need to avoidMFC
that's is fine, on the other hand, if you need to be stuck withC++
standard, then useifstream
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
CodingLover wrote:
Is that GetFileName is standard C++. I'm confusing with those.
Nope. It is provided by
Win32 API
. If you just need to avoidMFC
that's is fine, on the other hand, if you need to be stuck withC++
standard, then useifstream
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeSince I start to learn C++, confusing with this. Is that best way to work with standard C++? Sometimes I feel that use of MFC easy than standard C++, and other way too. Appreciate your explanation on it.
I appreciate your help all the time... CodingLover :)
-
Since I start to learn C++, confusing with this. Is that best way to work with standard C++? Sometimes I feel that use of MFC easy than standard C++, and other way too. Appreciate your explanation on it.
I appreciate your help all the time... CodingLover :)
Using
C++
standard you write code that will execute also on different platforms (for instanceLinux
). On the other hand, if you need to exploitWindows
not-portable functionalities (like theGUI
components1) the you have to useWin32 API
orMFC
.GUI
development withMFC
is usually simpler. :) (1) There are exceptions: for instanceQT
[^] is a framework that enable you to write portableGUI
code.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Using
C++
standard you write code that will execute also on different platforms (for instanceLinux
). On the other hand, if you need to exploitWindows
not-portable functionalities (like theGUI
components1) the you have to useWin32 API
orMFC
.GUI
development withMFC
is usually simpler. :) (1) There are exceptions: for instanceQT
[^] is a framework that enable you to write portableGUI
code.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeOk, as I said now I have mixture of them. If I want to learn standard C++ from where I should begin.
I appreciate your help all the time... CodingLover :)
-
Ok, as I said now I have mixture of them. If I want to learn standard C++ from where I should begin.
I appreciate your help all the time... CodingLover :)
What about "The C++ Programming Language" [^] book of Stroustrup? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
What about "The C++ Programming Language" [^] book of Stroustrup? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThanks a lot. :)
I appreciate your help all the time... CodingLover :)