c语言操作大于2G文本
2021-03-04 本文已影响0人
一路向后
1.源码实现
#define _FILE_OFFSET_BITS 64
#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<6144; k++)
//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 2m3.855s
user 1m48.128s
sys 0m5.541s