Public Group

# Write file from mem to hard disk.

This topic is 3541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I have a struct: struct Foo { int DataSize; char* Data; }; and the Data points to an allocated buffer somewhere in memory, how can I write that whole struct foo to file using ofstream? [Please do not mark threads 'solved' -- jpetrie] [Edited by - jpetrie on January 9, 2009 10:44:23 AM]

##### Share on other sites
It is fairly simple
Foo n;// ... blah to fill your structure{ofstream ofile("file", ios::binary);ofile.write(n.DataSize, sizeof(int));ofile.write(n.Data,n.DataSize);}

Foo n;ifstream ifile("file", ios::binary);ifile.read(n.DataSize, sizeof(int));n.Data = new char[n.DataSize];ifile.read(n.Data,n.DataSize);

##### Share on other sites
Perfect thanks, I thought about doing that but for some reason thought that "new" allocations weren't guaranteed to be contiguous.

##### Share on other sites
Not only does the language guarantee that the memory allocated by new[] is contiguous - it would have been impossible *not* to guarantee it. Consider: the return value from new[] is simply a pointer. If the memory were not contiguous, how could you possibly find out where the "other" chunks are so as to use them? :)

In any event, don't make a struct like that; just use std::vector<char>. You can serialize it the same way: write the size (which you can obtain from a vector with the .size() member function), then the data within the allocation (you can get a pointer to the vector's underlying allocation by taking the address of the first element, e.g. '&vec.front()'; the vector's underlying allocation is also guaranteed to be contiguous).

##### Share on other sites
Quote:
 Original post by ZahlmanNot only does the language guarantee that the memory allocated by new[] is contiguous - it would have been impossible *not* to guarantee it. Consider: the return value from new[] is simply a pointer. If the memory were not contiguous, how could you possibly find out where the "other" chunks are so as to use them? :)

And how can an object know which function to call based on it's type at runtime? Over the years I've been programming in C++, I learned one thing: trust nothing!

Quote:
 In any event, don't make a struct like that; just use std::vector. You can serialize it the same way: write the size (which you can obtain from a vector with the .size() member function), then the data within the allocation (you can get a pointer to the vector's underlying allocation by taking the address of the first element, e.g. '&vec.front()'; the vector's underlying allocation is also guaranteed to be contiguous).

Interesting, so how would I allocate this vector? A little sample would be appreciated.

##### Share on other sites
1) depends on what you are doing. Chances are you can defer this to compile time with better design.

2)
std::vector::size_type sz = 0;
std::vector<char> data;
std::ofstream ofile("file", ios::binary);
data.resize(sz)

and you can do this to just read the whole file into a buffer:
std::copy(istream_iterator<char>(ifile), istream_iterator<char>(), back_inserter(data));

##### Share on other sites
Quote:
Original post by VanillaSnake21
Quote:
 In any event, don't make a struct like that; just use std::vector. You can serialize it the same way: write the size (which you can obtain from a vector with the .size() member function), then the data within the allocation (you can get a pointer to the vector's underlying allocation by taking the address of the first element, e.g. '&vec.front()'; the vector's underlying allocation is also guaranteed to be contiguous).

Interesting, so how would I allocate this vector? A little sample would be appreciated.

You wouldn't "allocate" the vector; you would just declare one, the same way you'd declare an int. I was talking about the vector's allocation - i.e. the memory that is allocated by the code that implements the vector - which is already written for you and provided by the standard library. All you care about is retrieving a pointer to that allocation, so that you can use the file I/O routines with it. (There are more sophisticated techniques using the standard library, too, but they're probably not worth the bother here.)

But yes, pointers can only point at one chunk. Whereas runtime polymorphism can be implemented in a few different ways. :)

##### Share on other sites
@KulSeran thanks for the sample

@Zahlman

I see, so pretty much what Kelsaran did.

Thanks everyone, the vector looks much cleaner then a char*. I'll bet it will also help prevent those nasty memory leaks since I don't have to allocate anything.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632591
• Total Posts
3007250

×