读书人

如何创建有大小的空文件

发布时间: 2013-01-01 14:04:19 作者: rapoo

怎么创建有大小的空文件?
我用下面的 不行呀.

FILE* f = fopen("d:/d.txt","ab");
if (f)
{
int iret = fseek(f,1024 * 1024 * 1024 - 1,SEEK_SET);
fwrite("",1,1,f);
fclose(f);
}

[解决办法]
引用:
C/C++ code??12345678FILE* f = fopen("d:/d.txt","w"); if (f) { const long filesize = 1024 * 1024 ; int iret = fseek(f, filesize - 1,SEEK_SET); fputc(EOF, f……


msdn:
When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out.

你一旦写EOF的时候就回到原位置了。行不通的,可用其他方法变通下实现
[解决办法]
引用:
引用:C/C++ code??12345678FILE* f = fopen("d:/d.txt","w"); if (f) { const long filesize = 1024 * 1024 ; int iret = fseek(f, filesize - 1,SEEK_SET); ……

#include<stdio.h>
void main(int argc,char *argv[])
{FILE *fp=fopen("h:/d.txt","ab+");
int a=0,b,i;
while(getc(fp)!=EOF)
a++;
for(i=1;i<1024*1024-a;i++)
putc(0,fp);
fclose(fp);


}

比较笨,但感觉实现了你说的、、、

读书人网 >C语言

热点推荐