1

I've created a program in C++ that prompts the user for a filename and for the requested filesize. The program checks if the requested filesize is bigger than the actual filesize and then adds null characters (the ones with code 0) at the end of the file, until the requested filesize is reached.
I have done it like this (with fstream):

for (blah blah) {
    file << '\0'; // ('file' is an fstream object)
}

and it worked just as I wanted it to. I know this is a bad code as it may torture the hard disk by sending many calls to it (and it's slow by the way). This was only for testing reasons. Then, because of this problem I decided to create a variable storing the NUL characters and save the whole variable to the file at once (without saving each character separately).
Then the problem appeared... because of the NUL character (also known as null-terminator), terminating the string, I couldn't store those zeros in any way.
I've tried an array of chars, a string and even a stringstream, but none worked (there's something interesting about stringstream, when I used it, the file's content looked like this: 0x47c274). Anyway, it didn't work as I expected it to.
Is there any efficient way of storing an array of null characters?

rhino
  • 13,543
  • 9
  • 37
  • 39
  • 4
    Just an advice, do refer to the ASCII 0 as NUL and not as NULL. NULL ist the actual nullpointer and NUL ('\NUL') is the ASCII 0. – LukeN Oct 03 '10 at 00:47
  • 1
    I guess that in case of stringstream you sent a pointer to it to the stream instead of the object itself. Also, a string should work, it may contain \0 without problems. – liori Oct 03 '10 at 01:00
  • I see now; my problem was in saving it to the file, not with storing it :) – rhino Oct 03 '10 at 01:06
  • Note that the slowness was due to the many virtual calls inside iostreams, not a large number of I/O operations. `fstream` does buffer the data, at least :vP . Also, see my comment on Hugo's answer. – Potatoswatter Oct 11 '10 at 09:59

2 Answers2

3

Store them in an array of characters and use ostream::write to write the array to the file.

Turtle
  • 1,320
  • 10
  • 11
  • Thank you, it works the way with ofstream::write :) I've accepted Hugo Peixoto's answer, but your one is also good. ;) – rhino Oct 03 '10 at 00:54
2

Here's an example:

#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ofstream fout("pokemon");
  char buffer[1000];
  std::fill(buffer, buffer + 1000, '\0');
  fout.write(buffer, sizeof(char) * 1000);
  return 0;
}
Hugo Peixoto
  • 3,604
  • 2
  • 18
  • 12
  • Thanks! I'm just curious, why did you include algorithm? `std::fill` isn't declared there, is it? – rhino Oct 03 '10 at 00:55
  • std::fill is declared in a common algorithms file, which is probably included by iostream and/or fstream already. If you just want to use fill, the header to include is algorithm, but in this case it wasn't needed. – Hugo Peixoto Oct 03 '10 at 01:02
  • @Potatoswatter, what is the difference? It looks different, but I think it does the same, doesn't it? – rhino Oct 11 '10 at 10:02
  • @rhino: It's shorter and it doesn't require ``. – Potatoswatter Oct 11 '10 at 10:06