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. 2 questions about working with files

2 questions about working with files

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonoophelpquestion
4 Posts 3 Posters 1 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.
  • E Offline
    E Offline
    Ernesto D
    wrote on last edited by
    #1

    Hi!, i hope you guys can help me out. 1.- to get the size of a file (i dont use mfc), i use something like: HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL); if(hfile) { DWORD fsize=GetFileSize(hfile, NULL); CloseHandle(hfile); } is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable) 2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible thanks!

    G B 2 Replies Last reply
    0
    • E Ernesto D

      Hi!, i hope you guys can help me out. 1.- to get the size of a file (i dont use mfc), i use something like: HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL); if(hfile) { DWORD fsize=GetFileSize(hfile, NULL); CloseHandle(hfile); } is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable) 2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible thanks!

      G Offline
      G Offline
      Graham Bradshaw
      wrote on last edited by
      #2

      1. To get the size of a file, take a look at the _stat function, and the st_size member of the structure it populates. 2. Will an integer overflow? Depends on the compiler. The ANSI standard basically says that an integer can be whatever size the compiler manufacturer wants it to be. In early versions of Visual C++, targetting Windows 3, it was 16 bits. In a specialist compiler for an embedded system, it might only be 8 bits. In Visual C++ 6, its 32 bits. So provided you are using a modern version of Visual C++, you shouldn't have a problem. 3. Not a huge expert on non-Windows C++ development, but I would imagine that anything that uses the STL would be portable.

      E 1 Reply Last reply
      0
      • G Graham Bradshaw

        1. To get the size of a file, take a look at the _stat function, and the st_size member of the structure it populates. 2. Will an integer overflow? Depends on the compiler. The ANSI standard basically says that an integer can be whatever size the compiler manufacturer wants it to be. In early versions of Visual C++, targetting Windows 3, it was 16 bits. In a specialist compiler for an embedded system, it might only be 8 bits. In Visual C++ 6, its 32 bits. So provided you are using a modern version of Visual C++, you shouldn't have a problem. 3. Not a huge expert on non-Windows C++ development, but I would imagine that anything that uses the STL would be portable.

        E Offline
        E Offline
        Ernesto D
        wrote on last edited by
        #3

        Thanks Graham!

        1 Reply Last reply
        0
        • E Ernesto D

          Hi!, i hope you guys can help me out. 1.- to get the size of a file (i dont use mfc), i use something like: HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL); if(hfile) { DWORD fsize=GetFileSize(hfile, NULL); CloseHandle(hfile); } is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable) 2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible thanks!

          B Offline
          B Offline
          bolivar123
          wrote on last edited by
          #4

          another way to get the file size (and it's portable too) is to use the ftell function: FILE *f = fopen("foo.dat","rb"); long lFileSize = 0; if (f != NULL) { fseek(f,0,SEEK_END); lFileSize = ftell(f); fclose(f); }

          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