12

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!

OmegaTwig
  • 243
  • 1
  • 4
  • 15
  • 2
    How do you think `fputs` knows how many bytes to write? – CB Bailey Oct 25 '11 at 21:21
  • fwrite, or seek/putc('0') will be your easiest bet. – Mooing Duck Oct 25 '11 at 21:24
  • Don't forget to handle errors (e.g. check `argc`) – sehe Oct 25 '11 at 21:31
  • 1
    @MooingDuck: I think the teacher will take a red marker, and correct `putc('0')` into `putc(0)` or `putc('\0')`. At which time, the student should take the red marker, and strongly cross out the `++` signs appearing in the name of this course :) – sehe Oct 25 '11 at 21:52

3 Answers3

24

Potentially sparse file

This creates output.img of size 300 MB:

#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.

Dense file - filled with 0's

Functionally identical to the above, but filling the file with 0's:

#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;
        }
    }
}
einpoklum
  • 118,144
  • 57
  • 340
  • 684
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Added a three line solution that will save space by making a sparse file. – sehe Oct 25 '11 at 21:30
  • This works, but I don't understand the 20 in the ofs.seekp((300<<20)-1); I understand that it gets you to the size-1 position to write your empty character, but what does the 300<<20 operator do? – OmegaTwig Oct 25 '11 at 22:13
  • 3
    `<<` is the [bitwise shift-left operation](http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_C.2C_C.2B.2B.2C_C.23). Shifting binary numbers left by one bit is equivalent to multiplying by 2. So shifting left by 10 bits is equivalent to multiplying by 1024 (2**10). It is a convenient shorthand in absense of [C++11 userdefined literals like `300Mb`](http://stackoverflow.com/questions/237804/user-defined-literals-in-c11-a-much-needed-addition-or-making-c-even-more-b/237821#237821) – sehe Oct 25 '11 at 22:17
  • Thanks! Totally forgot about bit shifting :S – OmegaTwig Oct 26 '11 at 11:41
2

Your code doesn't work because you are using fputs which writes a null-terminated string into the output buffer. But you are trying to write all nulls, so it stops right when it looks at the first byte of your string and ends up writing nothing.

Now, to create a file of a specific size, all you need to do is to call truncate function (or _chsiz for Windows) exactly once and set what size you want the file to be.

Good luck!

0

To make a 2MB file you have to seek to 2*1024*1024 and write 0 bytes. fput()ting empty string will do no good no matter how many time. And the string is empty, because strings a 0-terminated.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • writing 0 bytes (e.g. `ofstream::write("", 0)`) doesn't work. – einpoklum Jun 28 '20 at 13:07
  • I do not think `ofstream` guarantees writing 0 bytes, honestly I have no idea how should one go about it with `ofstream` other than taking `fd` from it and performing the magic on it. Honestly, I don't think setting the size of the file is very "streaming" activity. – Michael Krelin - hacker Jun 28 '20 at 19:47