0

I am serializing some data to a file like this:

vector<ByteFeature>::iterator it = nByteFeatures.Content().begin();
for (;it != nByteFeatures.Content().end(); ++it)
{
    for ( int i = 0; i < 52; i++)
    {
        fwrite( &it->Features[i], sizeof(unsigned char), 1, outfile);
    }
}   

But I would like to know in advance how much bytes that will be in the file. I would like to write this number in front of the actual data. Because in some situations I will have to skip loading this data, and I need to know how many bytes I have to skip.

There is more data written to the disk, and it would be crucial to me that I can write the number of bytes directly before the actual data. I do not want to store this number in a separate file or so.

 .Content.size() would only tell me how many "items" are in there, but not the actual size of the data.

Thank you.

Stals
  • 1,453
  • 3
  • 26
  • 50
tmighty
  • 8,222
  • 19
  • 78
  • 182
  • 2
    since you're writing 52 bytes for each item, would `nByteFeatures.Content().size() * 52` not do the trick ? – Sander De Dycker May 06 '13 at 13:48
  • 1
    Why not using `sizeof()` on your data type, then multiplied by `.Content.size()` ? (i.e., `sizeof(ByteFeature)*nByteFeatures.Content().size()`) – JBL May 06 '13 at 13:48
  • You can always write a dummy size, then write the data, then `fseek` back to the beginning and put the real size in place of the dummy. – Roger Rowland May 06 '13 at 13:50
  • Or just read the size of the file directly rather than embedding it in the file: http://stackoverflow.com/questions/5840148/how-can-i-get-a-files-size-in-c – Doug Moscrop May 06 '13 at 13:51
  • @Doug: He said "in some situations I will have to skip loading this data" so I'm presuming there's more data after, i.e. the size of the file doesn't help directly. – someguy May 06 '13 at 13:53
  • Ideally, you are going to need to know the size of your data before writing. –  May 06 '13 at 14:01

2 Answers2

4

I've had to do this before myself.

The approach I took was to write a 4-byte placeholder, then the data, then fseek() back to the placeholder to write the length.

  • Write a 4-byte placeholder to the file.
  • ftell() to get the current file position.
  • Write the data to the file.
  • ftell() to get the new position.
  • Compute the length: the difference between the two ftell() values.
  • Then fseek() back to the placeholder and write the length.
Andy Thomas
  • 78,842
  • 10
  • 93
  • 142
  • Hmm, 4 bytes seems a little small. I suppose this has to do with the application. For instance, writing the file size is commonly used in the NITF file format (it uses 12 bytes) but the file size isn't at the beginning. It's part of the format in the file header. Presumably, one could only open part of the file to eventually get to the file size. Not sure how much you time you are saving by just putting it at the front like the OP wants. –  May 06 '13 at 13:57
  • The size will fit in an "int". Can you please add some code for "write the length."? That would be great! – tmighty May 06 '13 at 14:11
  • @Andy Thomas-Cramer The size will fit in an "int". Can you please add some code? That would be great! I am just so afraid to mess anything up, and I have spent have the day with tracking down a bug. That would really be very nice. – tmighty May 06 '13 at 14:19
  • @0A0D - In my case 4 bytes was sufficient. Larger files could require more. tmighty, I've added pseudocode above. If you have any questions about any parts of it, let me know. – Andy Thomas May 06 '13 at 14:36
  • @Andy Thomas-Cramer I mean if I use fwrite, will it overwrite? Any cave-ats? I was hoping to get some sample code so that I don't mess anything up. – tmighty May 06 '13 at 17:37
  • If you fseek() back to the placehold and fwrite(), the placeholder will be overwritten. For writing the length, try fwrite( &length, sizeof(long), 1, out ). If you want the file to be portable among multiple platforms with different word orders, consider using htonl() and ntohl(). Also, prefer to use sizeof(long) instead of "4" in the pseudocode above. – Andy Thomas May 06 '13 at 17:54
2

You are writing 52 unsigned chars to a file for every ByteFeature. So the total number of bytes you are writing is 52 * nByteFeatures.Contents().size(), assuming that one char equals one byte.

rwols
  • 2,708
  • 1
  • 17
  • 23