Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get the file handle of an ifstream

How to get the file handle of an ifstream

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vancouver
    wrote on last edited by
    #1

    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.

    CPalliniC 1 Reply Last reply
    0
    • V Vancouver

      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.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Vancouver wrote:

      '_file' : is not a member of 'ifstream'

      that's true, since _fileno is available for c-like streams only and you are using c++ 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.

      In testa che avete, signor di Ceprano?

      V 1 Reply Last reply
      0
      • CPalliniC CPallini

        Vancouver wrote:

        '_file' : is not a member of 'ifstream'

        that's true, since _fileno is available for c-like streams only and you are using c++ 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.

        V Offline
        V Offline
        Vancouver
        wrote on last edited by
        #3

        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?

        CPalliniC 1 Reply Last reply
        0
        • V Vancouver

          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?

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          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.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups