C ++创建一个特定大小的文件 [英] C++ Make a file of a specific size

查看:312
本文介绍了C ++创建一个特定大小的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的问题:我试图在C + +中创建一个x MB的文件。用户将输入文件名,然后输入一个介于5和10之间的数字作为他们想要创建的文件的大小。后来在这个项目中,我要做其他的事情,但我坚持的第一步创建的东西。



我的问题代码):

  char empty [1024]; 
for(int i = 0; i <1024; i ++)
{
empty [i] = 0;
}

fileSystem = fopen(argv [1],w +);


for(int i = 0; i <1024 * fileSize; i ++){
int temp = fputs(empty,fileSystem)
if(temp> -1){
//成功!
}
else {
cout<<error<< endl;
}
}

现在如果我正确地做我的数学1 char是1byte。在1KB中有1024字节,在MB中有1024KB。所以如果我想要一个2 MB的文件,我必须写入1024 * 1024 * 2字节到这个文件。是的?



我没有遇到任何错误,但我最终得到一个0字节的文件...我不知道我在这里做错了什么


解决方案



编辑这是一种更短的变体,功能上相同(示例创建 output.img 300Mb):

  #include< fstream> 

int main()
{
std :: ofstream ofs(ouput.img,std :: ios :: binary | std :: ios :: out);
ofs.seekp((300 << 20) - 1);
ofs.write(,1);
}

请注意,从技术上讲,这将是一个很好的方式来触发你文件系统对稀疏文件的支持






  #include< iostream> ; 
#include< fstream>
#include< vector>

int main()
{
std :: vector< char>空(1024,0);
std :: ofstream ofs(ouput.img,std :: ios :: binary | std :: ios :: out);

for(int i = 0; i <1024 * 300; i ++)
{
if(!ofs.write(& empty [0],empty.size ()))
{
std :: cerr<< 写入文件的问题< std :: endl;
return 255;
}
}
}


Here is my current problem: I am trying to create a file of x MB in C++. The user will enter in the file name then enter in a number between 5 and 10 for the size of the file they want created. Later on in this project i'm gonna do other things with it but I'm stuck on the first step of creating the darn thing.

My problem code (so far):

        char empty[1024];
        for(int i = 0; i < 1024; i++)
        {
            empty[i] = 0;
        }

        fileSystem = fopen(argv[1], "w+");


        for(int i = 0; i < 1024*fileSize; i++){
            int temp = fputs(empty, fileSystem);
            if(temp > -1){
                //Sucess!
            }
            else{
                cout<<"error"<<endl;
            }
        }

Now if i'm doing my math correctly 1 char is 1byte. There are 1024 bytes in 1KB and 1024KB in a MB. So if I wanted a 2 MB file, i'd have to write 1024*1024*2 bytes to this file. Yes?

I don't encounter any errors but I end up with an file of 0 bytes... I'm not sure what I'm doing wrong here so any help would be greatly appreciated!

Thanks!

解决方案

Here is what things would look like in actual c++

Edit Here is a way shorter variant that does functionally the same (sample creates output.img of 300Mb):

#include <fstream>

int main()
{
    std::ofstream ofs("ouput.img", std::ios::binary | std::ios::out);
    ofs.seekp((300<<20) - 1);
    ofs.write("", 1);
}

Note that technically, this will be a good way to trigger your filesystem's support for sparse files.


#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::vector<char> empty(1024, 0);
    std::ofstream ofs("ouput.img", std::ios::binary | std::ios::out);

    for(int i = 0; i < 1024*300; i++)
    {
        if (!ofs.write(&empty[0], empty.size()))
        {
            std::cerr << "problem writing to file" << std::endl;
            return 255;
        }
    }
}

这篇关于C ++创建一个特定大小的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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