如何创建“x”文件尺寸? [英] How to create file of "x" size?

查看:106
本文介绍了如何创建“x”文件尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c中设置文件的大小?我会打这个吗?

解决方案

是的,你会在fopen之后做到这一点 - 你可以创建所谓的稀疏文件

  #include< stdio.h> 
int main(void){
int X = 1024 * 1024 - 1;
FILE * fp = fopen(myfile,w);
fseek(fp,X,SEEK_SET);
fputc('\0',fp);
fclose(fp);



$ b $ p
$ b这个应该为你创建一个X字节的文件,情况下是1MB


How do I set the size of a file in c ? Would I do this after I fopen?

解决方案

Yes you would do it after fopen - you can create what is know as a sparse file

#include <stdio.h>
int main(void) {
        int X = 1024 * 1024 - 1;
        FILE *fp = fopen("myfile", "w");
        fseek(fp, X , SEEK_SET);
        fputc('\0', fp);
        fclose(fp);
}

That should create you a file for X Byte for whatever you need, in this case it's 1MB

这篇关于如何创建“x”文件尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆