How to get the file handle of an ifstream
-
When I try to get the file handle of an ifstream with _fileno, I receive the compile error '_file' : is not a member of 'ifstream' How else can I get the handle of an ifstream? At the moment I need the handle to get the file length by _filelength. How else can I get the length of an open file? I want to avoid using CFileFind, as the file is open anyway.
-
When I try to get the file handle of an ifstream with _fileno, I receive the compile error '_file' : is not a member of 'ifstream' How else can I get the handle of an ifstream? At the moment I need the handle to get the file length by _filelength. How else can I get the length of an open file? I want to avoid using CFileFind, as the file is open anyway.
Vancouver wrote:
'_file' : is not a member of 'ifstream'
that's true, since
_fileno
is available for c-like streams only and you are usingc++
streams. Give a look to the following code snippet:streampos size;
ifstream ifs("foo.txt");
ifs.seekg(0, ios::end);
size = ifs.tellg();
ifs.seekg(0, ios::beg);
size -= ifs.tellg();hope that helps. :)
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.
-
Vancouver wrote:
'_file' : is not a member of 'ifstream'
that's true, since
_fileno
is available for c-like streams only and you are usingc++
streams. Give a look to the following code snippet:streampos size;
ifstream ifs("foo.txt");
ifs.seekg(0, ios::end);
size = ifs.tellg();
ifs.seekg(0, ios::beg);
size -= ifs.tellg();hope that helps. :)
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.
It is like scratching the right ear with the left hand. The file length is known and recorded somewhere during open. seekg with ios::end uses exactly that information - but how to get it directly? I found in several sources, that the length info is not directly available; however, when looking around filebuf, I found the direct solution: 1. rdbuf delivers the pointer to filebuf 2. filebuf has the handle as file description 3. _filelength works with that file description. I tested it. Btw, I don't understand, why to substract the position of file beginning; is there any case, when that is not zero?
-
It is like scratching the right ear with the left hand. The file length is known and recorded somewhere during open. seekg with ios::end uses exactly that information - but how to get it directly? I found in several sources, that the length info is not directly available; however, when looking around filebuf, I found the direct solution: 1. rdbuf delivers the pointer to filebuf 2. filebuf has the handle as file description 3. _filelength works with that file description. I tested it. Btw, I don't understand, why to substract the position of file beginning; is there any case, when that is not zero?
Vancouver wrote:
It is like scratching the right ear with the left hand. The file length is known and recorded somewhere during open. seekg with ios::end uses exactly that information - but how to get it directly?
I agree, a lit cumbersome, but your way isn't that cleaner..., moreover I don't like such hacking-like actions with class data members (only matter of style).
Vancouver wrote:
Btw, I don't understand, why to substract the position of file beginning; is there any case, when that is not zero?
'cause, AFAIK nowhere (documentation) is stated that
tellg()
will return 0 at the beginning of the file. :)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.