Sign in to follow this  
Sybalos

ofstream write unsigned char*

Recommended Posts

Hi all, Im looking for a way to write unsigned char* on disk and read it back. How can i find its size/length. Does anybody have a correct way to do this with ofstream's write methode. My unsigned char* variable point to a bitmap data that i just load with freed(). Please post a solution for both read and write using ofstream from C++ standard. Thx.

Share this post


Link to post
Share on other sites
The size of an unsigned char* is generally 4 bytes. Writing it to a disk will do you no good, since the pointer will be invalid when you read it in from the file later.

I assume that what you meant to ask was how to write a text string to a file using ofstream. Text strings are not unsigned chars, they are regular old chars, or more correctly (since you asked for C++ standard) 'string' objects.

string str = "hello";
ofstream o;
o.open("junk.txt");
o << str;
o.close();

ifstream i;
string strIn;
i.open("junk.txt");
i >> strIn;
i.close();

cout << strIn;


Ta-da! Now, if you actually meant to write an array of runtime-allocated unsigned chars to disk, there's no standard way to know the length of the array just from the pointer to its first element.

What exactly is it you are doing?
[edit] Whoops! Missed part of your post--I just took Zahlman's signature line too seriously:
Quote:
As a general rule, if your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.



[Edited by - BeanDog on December 15, 2006 7:36:39 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Sybalos
My unsigned char* variable point to a bitmap data that i just load with freed(). Please post a solution for both read and write using ofstream from C++ standard.
Thx.


You could use ofstream.write, although you'll need to know the size. You should know the size anyway since passing around un-allocated pointers isn't exactly good practice.

Edit: Notice how in the example on the ofstream.write page, they get the size of the file and use that.

Edit2: For input, ifstream.read which takes the same parameters.

Share this post


Link to post
Share on other sites
You still can't just write the pointer; you have to write the pointed-at data.

If you want to write an actual bitmap file, then these have a specific format; look it up and see what things need to be written.

For the technical details of how to write things, try this reference.

If you're trying to design the file contents for yourself, you really should take this advice.

Oh, and read this, regardless.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sybalos
Scet, your link show almost wnat im looking. The problem is that ofsteam.write does not compile with an unsigned char* as 1st parameter


If memory serves, you have to cast whatever you pass to ofstream::write to const char *:


std::ofstream os("x.dat",std::ios::binary);
unsigned char *x=new unsigned char[23];

os.write(static_cast<const char*>(x),23);


I'm pretty sure that's right, although someone please correct me if not as I'm ashamed to admit I still use std::fwrite() et al for binary files.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this