美文网首页
c语言生成随机字符文本行

c语言生成随机字符文本行

作者: 一路向后 | 来源:发表于2021-03-04 22:22 被阅读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

相关文章

网友评论

      本文标题:c语言生成随机字符文本行

      本文链接:https://www.haomeiwen.com/subject/sfdlqltx.html