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 do you get the file size for an open file?

How do you get the file size for an open file?

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancequestion
4 Posts 2 Posters 5 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.
  • M Offline
    M Offline
    Member 4194593
    wrote on last edited by
    #1

    I have the following code:

    FILE \*fp = stdin;
    
    if ((fp = fopen(argv\[optind\], "rb")) == NULL) {
         printf("Cannot open file %s\\n", argv\[optind\]);
         return 2;
    }
    

    It executes correctly and the correct file is accessed. I added the following to get the file size:

    SizeLow = GetFileSize(\_fileno(fp), & SizeHigh);
    if ((SizeLow == 0xFFFFFFFF) && ((Error = GetLastError()) != NO\_ERROR)) {
         printf("Cannot read file size for %s, error %d\\n", argv\[optind\], Error);
         help();
         return 2;
    }
    if (binary && (SizeHigh > 0x70000000)) {
        printf("File too big for Binary Mode\\n");
        help();
        return 2;
    }
    \_\_asm {
              mov   eax,SizeLow
              mov   edx,SizeHigh
              cmp   binary,TRUE
              jnz   Bytes
              shld  edx,eax,3
              shl   eax,3
          Bytes:
              mov   DWORD PTR totalc,eax
              mov   DWORD PTR totalc+4,edx
          }
    

    When I execute I get the following:

    ent.exe f:\tst1.tst >ent1.txt
    Cannot read file size for f:\tst1.tst, error 6

    What am I doing wrong with the the GetFileSize call? In case you are wondering, this is a modification to John Walker's ENT to speed up execution for huge files. Dave.

    J 1 Reply Last reply
    0
    • M Member 4194593

      I have the following code:

      FILE \*fp = stdin;
      
      if ((fp = fopen(argv\[optind\], "rb")) == NULL) {
           printf("Cannot open file %s\\n", argv\[optind\]);
           return 2;
      }
      

      It executes correctly and the correct file is accessed. I added the following to get the file size:

      SizeLow = GetFileSize(\_fileno(fp), & SizeHigh);
      if ((SizeLow == 0xFFFFFFFF) && ((Error = GetLastError()) != NO\_ERROR)) {
           printf("Cannot read file size for %s, error %d\\n", argv\[optind\], Error);
           help();
           return 2;
      }
      if (binary && (SizeHigh > 0x70000000)) {
          printf("File too big for Binary Mode\\n");
          help();
          return 2;
      }
      \_\_asm {
                mov   eax,SizeLow
                mov   edx,SizeHigh
                cmp   binary,TRUE
                jnz   Bytes
                shld  edx,eax,3
                shl   eax,3
            Bytes:
                mov   DWORD PTR totalc,eax
                mov   DWORD PTR totalc+4,edx
            }
      

      When I execute I get the following:

      ent.exe f:\tst1.tst >ent1.txt
      Cannot read file size for f:\tst1.tst, error 6

      What am I doing wrong with the the GetFileSize call? In case you are wondering, this is a modification to John Walker's ENT to speed up execution for huge files. Dave.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      GetFileSize() expects a file HANDLE but you are passing a file descriptor returned by _fileno(). This is indicated by the error code 6 ERROR_INVALID_HANDLE. See A Handy Guide To Handling Handles[^] for the differences and how to convert between the various file handle types.

      M 2 Replies Last reply
      0
      • J Jochen Arndt

        GetFileSize() expects a file HANDLE but you are passing a file descriptor returned by _fileno(). This is indicated by the error code 6 ERROR_INVALID_HANDLE. See A Handy Guide To Handling Handles[^] for the differences and how to convert between the various file handle types.

        M Offline
        M Offline
        Member 4194593
        wrote on last edited by
        #3

        Jochen, Thank you for the excellent reference. Just what I needed. Dave.

        1 Reply Last reply
        0
        • J Jochen Arndt

          GetFileSize() expects a file HANDLE but you are passing a file descriptor returned by _fileno(). This is indicated by the error code 6 ERROR_INVALID_HANDLE. See A Handy Guide To Handling Handles[^] for the differences and how to convert between the various file handle types.

          M Offline
          M Offline
          Member 4194593
          wrote on last edited by
          #4

          Jochen, With this line, everything works correctly:

          SizeLow = GetFileSize(\_get\_osfhandle(\_fileno(fp)), & SizeHigh);
          

          Thanks for the pointer. Dave.

          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