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. C++ Get File Size

C++ Get File Size

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasejsonquestion
6 Posts 4 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.
  • J Offline
    J Offline
    Jim Fell
    wrote on last edited by
    #1

    Hello. I'm writing a program using M$ Visual C++. Is there a simple API for getting the file size? Something like GetFileSize? :) I'm trying to read out bytes from a binary file, and I think it's prematurely reading out an EOF character. So, I need to query the filesize from the OS. Thanks.

    R 1 Reply Last reply
    0
    • J Jim Fell

      Hello. I'm writing a program using M$ Visual C++. Is there a simple API for getting the file size? Something like GetFileSize? :) I'm trying to read out bytes from a binary file, and I think it's prematurely reading out an EOF character. So, I need to query the filesize from the OS. Thanks.

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      pcpro178 wrote:

      Is there a simple API for getting the file size? Something like GetFileSize?

      You mean something like GetFileSize()[^]? Yeah, there is.

      It is a crappy thing, but it's life -^ Carlo Pallini

      J 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        pcpro178 wrote:

        Is there a simple API for getting the file size? Something like GetFileSize?

        You mean something like GetFileSize()[^]? Yeah, there is.

        It is a crappy thing, but it's life -^ Carlo Pallini

        J Offline
        J Offline
        Jim Fell
        wrote on last edited by
        #3

        Well, yes, but GetFileSize requires a variable of type HANDLE to be passed to it. I have a FILE variable instantiated, but how do I go from one to the other?

        C J 2 Replies Last reply
        0
        • J Jim Fell

          Well, yes, but GetFileSize requires a variable of type HANDLE to be passed to it. I have a FILE variable instantiated, but how do I go from one to the other?

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          If you insist on the FILE pointer, you can do this:

          fseek(file, 0, SEEK_END);
          filesize = ftell(file);
          fseek(file, 0, SEEK_SET);

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

          1 Reply Last reply
          0
          • J Jim Fell

            Well, yes, but GetFileSize requires a variable of type HANDLE to be passed to it. I have a FILE variable instantiated, but how do I go from one to the other?

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            There are multiple ways including the following simplified code (will not compile):

            FILE* pFile = fopen();
            if (pFile)
            {
            int fileNum = _fileno(pFile)
            HANDLE hFile = (HANDLE) _get_osfhandle(fileNum);
            GetFileSize(hFile);
            }

            J 1 Reply Last reply
            0
            • J Joe Woodbury

              There are multiple ways including the following simplified code (will not compile):

              FILE* pFile = fopen();
              if (pFile)
              {
              int fileNum = _fileno(pFile)
              HANDLE hFile = (HANDLE) _get_osfhandle(fileNum);
              GetFileSize(hFile);
              }

              J Offline
              J Offline
              Jim Fell
              wrote on last edited by
              #6

              Thanks! This was a huge help! :)

              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