Creating a large Dummy File

I’m new to this community and also new to iOS development, and I hope that someone can help me with a little problem…

Currently I’m writing a little app for myself, that should create a very large dummy file, with dummy data or words or whatever it makes it big.

  • Converting NSData to Integer in Swift
  • How to convert AVAsset to NSData or save it to file manager
  • Retrieve nsdata from sqlite stored in Blob data type for iPhone App
  • Check type of class an NSData store?
  • Extract struct from NSData in Swift
  • Creating a base-64 string from NSData
  • The reason is, I would like to click a button and the app should generate a 4 gb file and save it to disk, that no other app could use it, because it isnt free… If I need the 4 gb of free space, I just have to open my little app, and after clicking another button, the file will be deleted, and i got 4 gb of free diskspace…

    I know how to write files to disk but I have no idea how to generate a large amount of data in short time, to make it 4 or more gb big???

    Does someone have an idea how to generate such a big file with a for loop or something?

    5 Solutions Collect From Internet About “Creating a large Dummy File”

    If you just want to occupy 4GB of disk space, then create a file, seek to 4GB and write a byte. This will create a “sparse file” containing next-to-no data in next-to-no time!

    This is C:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    ...
    
    int fd = open("/path/to/file", O_WRONLY, 0644);
    if (fd >= 0)
    {
        lseek(fd, 4 * 1024 * 1024 * 1024, SEEK_SET);
        write(fd, &fd, 1);
        close(fd);
    }
    

    EDIT Dropped creat() in favour of open() so the file mode can be specified.

    Also error checking should be better than shown…

    If you do rough calculation by 1000, not by 1024

     4 gb 
     4000 mb
     4000000 kb 
     4000000000 b 
    32000000000 bits
    

    And exactly it will be 34359738368 bits

    And if you go on to write that much characters (a b c etc) to a text file, by loop, you will create a file of 4gb.

    You can create an NSMutableData object and use -setLength: to expand it to 4GB. (-setLength will fill with zeros), before writing it out to a file.

    I’m not sure how well iOS will cope with an NSMutableData object that large, however.

    Edit: Example:

    NSString *filename; // The location to save your file
    NSMutableData *data = [[NSMutableData alloc] init];
    [data setLength:4*1024*1024*1024];
    [data writeToFile:fileName atomically:NO];
    

    Bear in mind also that iOS will purge your app’s data in certain directories if the device’s available storage is low.

    You can just write it in C, incrementally:

    // a starting point:
    const char path[] = "somewhere";
    const size_t NBytesInBuffer = 1024;
    /* fill buffer, if you like */
    const char buffer[NBytesInBuffer] = {0};
    FILE* const file = fopen(path, "w+");
    assert(file);
    const size_t ElementSize = sizeof(buffer[0]);
    
    uint64_t nBytesWritten = 0;
    while (UINT32_MAX > nBytesWritten) {
        nBytesWritten += ElementSize * fwrite(buffer, ElementSize, NBytesInBuffer, file);
    }
    
    fclose(file);
    

    Memory consumption will not be an issue here. Of course, it will take some time to write the entire file, as it is I/O bound. You will probably want some real data in there one day, too 🙂

    Windows:

    Open the command shell – cmd
    Execute the following command in order to create a file of 1mb

    fsutil file createnew ./test.tmp 1048576
    

    Linux:

    Use convert and copy command – dd

    dd if=/dev/zero of=test.tmp bs=1M count=1
    

    OS X & Solaris:

    mkfile 1m test.tmp