#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
FILE *fp;
int writeSize = 0;
int a = 1;
int fileSize = 0;
fp = fopen("test.txt", "w+");
if ( NULL == fp )
{
fprintf(stderr, "file create error!\n");
exit (1);
}
fseek(fp, 1024*1023L, SEEK_CUR);
writeSize = ftell(fp);
printf("file location: %d\n", writeSize);
writeSize = fwrite(&a, sizeof(a), 1024, fp);
fclose(fp);
fp = fopen("test.txt", "r");
if ( NULL == fp )
{
fprintf(stderr, "file open error!\n");
exit (1);
}
fseek(fp, SEEK_CUR, SEEK_END);
fileSize = ftell(fp);
printf("file size = %d\n", fileSize);
fclose(fp);
return 0;
}
通过fseek参加1M大小的文件,原理是定位到文件末尾处,再写入几个数据,就完成了文件的创建