创建一个大的虚拟文件 [英] Creating a large Dummy File

查看:68
本文介绍了创建一个大的虚拟文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个社区的新手,也是iOS开发的新手,我希望有人可以帮我解决一个小问题...

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.

原因是,我会喜欢单击一个按钮,应用程序应该生成一个4 GB的文件并将其保存到磁盘,没有其他应用程序可以使用它,因为它不是免费的...如果我需要4 GB的可用空间,我只需要打开我的小应用程序,点击另一个按钮后,该文件将被删除,我有4 GB的可用磁盘空间...

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...

我知道如何将文件写入磁盘但我不知道如何在短时间内生成大量数据,使其达到4个或更多gb大???

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???

有人知道如何生成这样一个带有for循环的大文件?

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

推荐答案

如果你只想占用4GB的磁盘空间, n创建一个文件,寻求4GB并写一个字节。这将创建一个稀疏文件,其中包含即将到来的无数据!

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!

这是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);
}

编辑已删除 creat ()赞成 open()所以可以指定文件模式。

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

错误检查也应该比显示的更好......

Also error checking should be better than shown...

这篇关于创建一个大的虚拟文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆