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++ Quick Filespace Allocation

C++ Quick Filespace Allocation

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
3 Posts 3 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.
  • X Offline
    X Offline
    x87Bliss
    wrote on last edited by
    #1

    I generally use what MSDN calls CRT lowlevel file I/O functions. These are the ones defined in io.h and fcntl.h; open, lseeki64, telli64, close, etc... There doesn't seem to be a function to allocate filespace quickly. For this example I would want to create a 10MB file. If I write 10485760 \0's to the file, it will obviously make the file 10MB - however it is slow (well for LARGE files). I did experiment by using the following code on winvista 32-bit. char c = 0; int newfile = open("newfile", _O_WRONLY | _O_BINARY | _O_TRUNC | _O_CREAT, _S_IREAD | _S_IWRITE); lseeki64(newfile, 10485759, SEEK_SET); //seek 1 byte short of 10MB write(newfile, &c, 1); //write \0 at this 10MB position. close(newfile); This seemed to do the trick, and the resulting 10MB file was \0 filled. However, I didn't think you were allowed to seek past the end of a file with lseek. Is this behavior expected to work on all OSs, or will some have an error with the seek? Is there any better way to do it? Basically what I am trying to accomplish is telling the OS that I am going to need X amount of disk space BEFORE I start writing the actual data, without taking up much time. The files are usually around 8GB, but may be even larger than that. My goal is to minimize fragmentation and the possibility of running out of disk space unexpectedly in the middle of writing to the file. Finally, the contents of the allocated file are unimportant and don't have to be \0 filled (i.e. garbage is ok), but will this way or an alternative guarantee \0 filling?

    D V 2 Replies Last reply
    0
    • X x87Bliss

      I generally use what MSDN calls CRT lowlevel file I/O functions. These are the ones defined in io.h and fcntl.h; open, lseeki64, telli64, close, etc... There doesn't seem to be a function to allocate filespace quickly. For this example I would want to create a 10MB file. If I write 10485760 \0's to the file, it will obviously make the file 10MB - however it is slow (well for LARGE files). I did experiment by using the following code on winvista 32-bit. char c = 0; int newfile = open("newfile", _O_WRONLY | _O_BINARY | _O_TRUNC | _O_CREAT, _S_IREAD | _S_IWRITE); lseeki64(newfile, 10485759, SEEK_SET); //seek 1 byte short of 10MB write(newfile, &c, 1); //write \0 at this 10MB position. close(newfile); This seemed to do the trick, and the resulting 10MB file was \0 filled. However, I didn't think you were allowed to seek past the end of a file with lseek. Is this behavior expected to work on all OSs, or will some have an error with the seek? Is there any better way to do it? Basically what I am trying to accomplish is telling the OS that I am going to need X amount of disk space BEFORE I start writing the actual data, without taking up much time. The files are usually around 8GB, but may be even larger than that. My goal is to minimize fragmentation and the possibility of running out of disk space unexpectedly in the middle of writing to the file. Finally, the contents of the allocated file are unimportant and don't have to be \0 filled (i.e. garbage is ok), but will this way or an alternative guarantee \0 filling?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      The only constraint I see with lseeki64() is that you can't position the offset before the beginning of the file. Another option would be to try SetFilePointer() and SetEndOfFile()?

      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • X x87Bliss

        I generally use what MSDN calls CRT lowlevel file I/O functions. These are the ones defined in io.h and fcntl.h; open, lseeki64, telli64, close, etc... There doesn't seem to be a function to allocate filespace quickly. For this example I would want to create a 10MB file. If I write 10485760 \0's to the file, it will obviously make the file 10MB - however it is slow (well for LARGE files). I did experiment by using the following code on winvista 32-bit. char c = 0; int newfile = open("newfile", _O_WRONLY | _O_BINARY | _O_TRUNC | _O_CREAT, _S_IREAD | _S_IWRITE); lseeki64(newfile, 10485759, SEEK_SET); //seek 1 byte short of 10MB write(newfile, &c, 1); //write \0 at this 10MB position. close(newfile); This seemed to do the trick, and the resulting 10MB file was \0 filled. However, I didn't think you were allowed to seek past the end of a file with lseek. Is this behavior expected to work on all OSs, or will some have an error with the seek? Is there any better way to do it? Basically what I am trying to accomplish is telling the OS that I am going to need X amount of disk space BEFORE I start writing the actual data, without taking up much time. The files are usually around 8GB, but may be even larger than that. My goal is to minimize fragmentation and the possibility of running out of disk space unexpectedly in the middle of writing to the file. Finally, the contents of the allocated file are unimportant and don't have to be \0 filled (i.e. garbage is ok), but will this way or an alternative guarantee \0 filling?

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

        Would GetDiskFreeSpaceEx or similar do the trick? I am not sure if your concern about fragmenting the disk is necessary. Why do you want to second guess the OS? Just my opinion of course. Vaclav

        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