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. creating empty file

creating empty file

Scheduled Pinned Locked Moved C / C++ / MFC
21 Posts 7 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.
  • S sarfaraznawaz

    large file without any data inside the file . look what actually looking to do is to create a large file with define size.

    P Offline
    P Offline
    pasztorpisti
    wrote on last edited by
    #8

    A file is either empty or large. It can not be both.

    1 Reply Last reply
    0
    • S sarfaraznawaz

      Hello , Is it possible to create a empty file with predefined size of that file. please reply me soon ... Regards Sarfaraz

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #9

      No and yes. No, insofar as the file must contain some data. The data will be meaningless, but it will be there all the same. Yes, in that you can create a file that occupies 1GB without explictly writing 1gb of data to it. I write a single byte after settiing the file-pointer. This is enough to create the file with the desired size. It takes just as long to run for 1024 bytes as it does for 1024 megabytes. Code:

      #include <stdio.h>

      int main()
      {
      FILE *fp;
      const int numMegabytes = 1024; // 1GB
      int numBytes = (numMegabytes * 1<<20);

      fp = fopen("myFile.dat", "wb");
      fseek(fp, numBytes-1, SEEK\_END); // -1 to account for extra byte we write
      fwrite("\\0", 1, 1, fp);             // write a single NULL byte to ensure file-size gets set correctly.
      fclose(fp);
      
      return 0;
      

      }

      "Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin

      P 1 Reply Last reply
      0
      • S sarfaraznawaz

        Hello , Is it possible to create a empty file with predefined size of that file. please reply me soon ... Regards Sarfaraz

        E Offline
        E Offline
        Erudite_Eric
        wrote on last edited by
        #10

        sarfaraznawaz wrote:

        please reply me soon ...

        Why, are you in a meeting and want to impress someone?

        R 1 Reply Last reply
        0
        • S sarfaraznawaz

          large file without any data inside the file . look what actually looking to do is to create a large file with define size.

          J Offline
          J Offline
          jeron1
          wrote on last edited by
          #11

          When you say 'empty', do you really mean a file filled with 0's or 1's?

          1 Reply Last reply
          0
          • S sarfaraznawaz

            large file without any data inside the file . look what actually looking to do is to create a large file with define size.

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #12

            Read the documentation of SetFilePointerEx()[^], SetEndOfFile()[^] and SetFileValidData()[^] - these are the functions you need along with CreateFile()[^] and CloseHandle()[^]. Create a file with CreateFile(), set the file pointer with SetFilePointerEx() and then call SetEndOfFile(). As a last step close the file with CloseHandle(). This is how you preallocate a file on windows. Note that the preallocated file data will be overwritten with zeros by windows in order to prevent you from reading the previous contents of the files that were stored on these sectors before deletion/moving. If you want to ask windows to skip the overwrite with zeros (for example to save time because in case of big preallocation its time consuming) then you need a few privileges and a SetFileValidData() call. Read the function documentations and use google to find out how to do that trick.

            S 1 Reply Last reply
            0
            • enhzflepE enhzflep

              No and yes. No, insofar as the file must contain some data. The data will be meaningless, but it will be there all the same. Yes, in that you can create a file that occupies 1GB without explictly writing 1gb of data to it. I write a single byte after settiing the file-pointer. This is enough to create the file with the desired size. It takes just as long to run for 1024 bytes as it does for 1024 megabytes. Code:

              #include <stdio.h>

              int main()
              {
              FILE *fp;
              const int numMegabytes = 1024; // 1GB
              int numBytes = (numMegabytes * 1<<20);

              fp = fopen("myFile.dat", "wb");
              fseek(fp, numBytes-1, SEEK\_END); // -1 to account for extra byte we write
              fwrite("\\0", 1, 1, fp);             // write a single NULL byte to ensure file-size gets set correctly.
              fclose(fp);
              
              return 0;
              

              }

              "Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin

              P Offline
              P Offline
              pasztorpisti
              wrote on last edited by
              #13

              The std solution. I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup. In DOS this trick created only a chain in the FAT without actually filling up the space so it was very quick. :-)

              enhzflepE 1 Reply Last reply
              0
              • P pasztorpisti

                The std solution. I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup. In DOS this trick created only a chain in the FAT without actually filling up the space so it was very quick. :-)

                enhzflepE Offline
                enhzflepE Offline
                enhzflep
                wrote on last edited by
                #14

                pasztorpisti wrote:

                I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup.

                :laugh: I'm in the middle of playing with a similar piece of fun to throw at a friend. I'm planning to port HIDache[^] to the arduino platform with a 'software-only' implementation of the usb protocol[^] to send random keystrokes and mouse-strokes to his pc (or in fact, any device with a usb host that supports HID kbs and mice). Got plenty of old usb memory stick enclosures that will hold an Arduino ProMini. Looks like being the most fun $5 or $6 I've spent in a long time. :evil-grin: Can't even begin to imagine the fun if I manage to sneak an NRF24L01+ or A7105 2.4ghz transceiver module in there too!

                "Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin

                P 1 Reply Last reply
                0
                • enhzflepE enhzflep

                  pasztorpisti wrote:

                  I tricked one of my friends in the old DOS times with an autoexec program that always filled up the hard drive free space at startup.

                  :laugh: I'm in the middle of playing with a similar piece of fun to throw at a friend. I'm planning to port HIDache[^] to the arduino platform with a 'software-only' implementation of the usb protocol[^] to send random keystrokes and mouse-strokes to his pc (or in fact, any device with a usb host that supports HID kbs and mice). Got plenty of old usb memory stick enclosures that will hold an Arduino ProMini. Looks like being the most fun $5 or $6 I've spent in a long time. :evil-grin: Can't even begin to imagine the fun if I manage to sneak an NRF24L01+ or A7105 2.4ghz transceiver module in there too!

                  "Science adjusts its views based on what's observed. Faith is the denial of observation, so that belief can be preserved." - Tim Minchin

                  P Offline
                  P Offline
                  pasztorpisti
                  wrote on last edited by
                  #15

                  :-) :-) :-)

                  1 Reply Last reply
                  0
                  • P pasztorpisti

                    Read the documentation of SetFilePointerEx()[^], SetEndOfFile()[^] and SetFileValidData()[^] - these are the functions you need along with CreateFile()[^] and CloseHandle()[^]. Create a file with CreateFile(), set the file pointer with SetFilePointerEx() and then call SetEndOfFile(). As a last step close the file with CloseHandle(). This is how you preallocate a file on windows. Note that the preallocated file data will be overwritten with zeros by windows in order to prevent you from reading the previous contents of the files that were stored on these sectors before deletion/moving. If you want to ask windows to skip the overwrite with zeros (for example to save time because in case of big preallocation its time consuming) then you need a few privileges and a SetFileValidData() call. Read the function documentations and use google to find out how to do that trick.

                    S Offline
                    S Offline
                    sarfaraznawaz
                    wrote on last edited by
                    #16

                    ok. i think its not possible to have empty file except filled with zeros... Thanks every body for your replies. Looking to make app that lock the usb data but am unable to start any idea or guidance will be appreciated . regards sarfaraz

                    1 Reply Last reply
                    0
                    • S sarfaraznawaz

                      large file without any data inside the file . look what actually looking to do is to create a large file with define size.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #17

                      Hi, Yes, you can create 'empty' files of 1GB size. If you are on Microsoft Windows XP or above and using an NTFS file system then you can create a sparse file. This will allow you to create 'empty' files of any size that contain 'virtual zeros'. The Windows operating system will report the file as having a valid file size... and it will even apply to user disk quota. Sparse Files[^] Steps to create a sparse file: 1.) Call CreateFile [^]and create a new file. Keep the file handle open. 2.) Call DeviceIoControl [^] on your open file handle with the FSCTL_SET_SPARSE control code[^] to mark the file as sparse. 3.) Call the SetFilePointerEx function[^] to move from FILE_BEGIN past the end of file. Make sure to assign liDistanceToMove argument to the desired file size. 4.) Call the SetEndOfFile function[^] to set the EOF to the current position of the file pointer. 5.) Close your file hande. Best Wishes, -David Delaune

                      1 Reply Last reply
                      0
                      • E Erudite_Eric

                        sarfaraznawaz wrote:

                        please reply me soon ...

                        Why, are you in a meeting and want to impress someone?

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

                        I know you're helpful here normally, but that's not really an answer, Eric. :)

                        "Real men drive manual transmission" - Rajesh.

                        E 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          I know you're helpful here normally, but that's not really an answer, Eric. :)

                          "Real men drive manual transmission" - Rajesh.

                          E Offline
                          E Offline
                          Erudite_Eric
                          wrote on last edited by
                          #19

                          If a post offends me then I will reply in an appropriate manner. :) By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person. :)

                          S R 2 Replies Last reply
                          0
                          • E Erudite_Eric

                            If a post offends me then I will reply in an appropriate manner. :) By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person. :)

                            S Offline
                            S Offline
                            sarfaraznawaz
                            wrote on last edited by
                            #20

                            Can we create the virtual drive of usb .

                            1 Reply Last reply
                            0
                            • E Erudite_Eric

                              If a post offends me then I will reply in an appropriate manner. :) By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person. :)

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

                              Erudite_Eric wrote:

                              If a post offends me then I will reply in an appropriate manner. :)

                              Yeah, I've done that before, but I found that to be of useful to nobody. I thought I'd just point it out, but you're free to do what you feel is right.

                              Erudite_Eric wrote:

                              By the way my real name is fat_boy, but that account got closed for pointing out obvious truths about a certain person.

                              Now, you're being offensive. Because I consider myself as a regggular here. :) Anyone who's been here long enough (and regular) will know your story. But it has nothing to do with the icebergs melting, I tell you.

                              "Real men drive manual transmission" - Rajesh.

                              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