c语言生成随机字符文本行
2021-03-04 本文已影响0人
一路向后
1.源码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
FILE *fp = NULL;
char buf[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,,./?\\()*&^%$#@!~`\"{}[]|<>";
char tmp[1024*1024];
int a = 0;
int i, j, k;
int len = strlen(buf);
srand((unsigned)time(NULL));
fp = fopen("1.txt", "wb");
if(fp == NULL)
{
return -1;
}
for(k=0; k<1024; k++)
{
for(j=0; j<1024; j++)
{
for(i=0; i<1023; i++)
{
a = rand() % len;
tmp[1024*j+i] = buf[a];
}
tmp[1024*j+i] = '\n';
}
fwrite(tmp, 1024*1024, 1, fp);
}
fclose(fp);
return 0;
}
2.编译源码
$ gcc -o example example.c
3.运行程序及其结果
$ time ./example
real 0m20.922s
user 0m16.082s
sys 0m1.975s