与在Linux系统上快速创建大文件类似,我想在Windows系统上快速创建一个大文件。我想要的大小是5GB,内容并不重要。最好使用内置命令或短批处理文件,但如果没有其他简单的方法,我也可以接受应用程序。
与在Linux系统上快速创建大文件类似,我想在Windows系统上快速创建一个大文件。我想要的大小是5GB,内容并不重要。最好使用内置命令或短批处理文件,但如果没有其他简单的方法,我也可以接受应用程序。
使用:
/*
Creates an empty file, which can take all of the disk
space. Just specify the desired file size on the
command line.
*/
#include <windows.h>
#include <stdlib.h>
int main (int argc, char* ARGV[])
{
int size;
size = atoi(ARGV[1]);
const char* full = "fulldisk.dsk";
HANDLE hf = CreateFile(full,
GENERIC_WRITE,
0,
0,
CREATE_ALWAYS,
0,
0);
SetFilePointer(hf, size, 0, FILE_BEGIN);
SetEndOfFile(hf);
CloseHandle(hf);
return 0;
}
我发现了一个非常好用的工具,可以在https://github.com/acch/genfiles上进行配置。
它可以使用随机数据填充目标文件,因此不会出现稀疏文件的问题。对于我的目的(测试压缩算法),它产生了很好的白噪声水平。
这是一个普通的C语言代码...可以在Windows XX上使用MinGW GCC编译,并且应该可以在任何“通用”的C环境下工作。
它生成一个指定大小的空文件。生成的文件并不仅仅是一个占用目录空间的条目,而实际上占用了指定数量的字节。由于除了在关闭前写入的字节外,没有实际写操作,因此速度很快。
我的实例生成一个全是零的文件 - 这可能因平台而异; 该程序本质上为待处理数据设置目录结构。
#include <stdio.h>
#include <stdlib.h>
FILE *file;
int main(int argc, char **argv)
{
unsigned long size;
if(argc!=3)
{
printf("Error ... syntax: Fillerfile size Fname \n\n");
exit(1);
}
size = atoi(&*argv[1]);
printf("Creating %d byte file '%s'...\n", size, &*argv[2]);
if(!(file = fopen(&*argv[2], "w+")))
{
printf("Error opening file %s!\n\n", &*argv[2]);
exit(1);
}
fseek(file, size-1, SEEK_SET);
fprintf(file, "%c", 0x00);
fclose(file);
}
临时文件应存储在Windows临时文件夹中。根据Rod的答案,您可以使用以下一行代码创建一个大小为5 GB的临时文件,并返回文件名。
[System.IO.Path]::GetTempFileName() | % { [System.IO.File]::Create($_).SetLength(5gb).Close;$_ } | ? { $_ }
说明:
[System.IO.Path]::GetTempFileName()
会在 Windows 临时文件夹中生成一个随机的文件名和扩展名。[System.IO.File]::Create($_)
,该命令将创建文件。.SetLength(5gb)
将文件名设置为新创建的文件。令人惊讶的是,PowerShell 支持字节转换,这非常有帮助。.close
关闭文件句柄以允许其他应用程序访问它。;$_
返回文件名,并使用 | ? { $_ }
确保只返回文件名,而不是由 [System.IO.File]::Create($_)
返回的空字符串。...在几秒钟内上传一个大小为1 MB的dummy.txt文件。
echo "This is just a sample line appended to create a big file.. " > dummy.txt
for /L %i in (1,1,14) do type dummy.txt >> dummy.txt
#include<stdlib.h>
#include<iostream>
#include<conio.h>
#include<fstream>
#using namespace std;
int main()
{
int a;
ofstream fcout ("big_file.txt");
for(;;a += 1999999999){
do{
fcout << a;
}
while(!a);
}
}
cat out.txt >> out.txt
等待足够长的时间,使文件变得足够大。然后按下Ctrl-C结束它。
cat: ..txt: input file is output file
。 - sdgfsdhcat dummy1.txt >> dummy2.txt && cat dummy2.txt >> dummy1.txt
- sdgfsdh在Python中简单的回答:如果你需要创建一个大的真实文本文件,我只是使用了一个简单的while
循环,在大约20秒钟内就能够创建一个5 GB的文件。我知道这很粗糙,但它足够快。
outfile = open("outfile.log", "a+")
def write(outfile):
outfile.write("hello world hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world"+"\n")
return
i=0
while i < 1000000:
write(outfile)
i += 1
outfile.close()
int
?这对于一个5 GB的文件有效吗?这是否限制在2或4 GB?SetFilePointer()
的第三个参数为0表明它不适用于5 GB的文件。 - Peter Mortensen