PDA

View Full Version : using char *buffer with generic content in c?


Koepi
20th May 2002, 14:06
Ahoy,

This is a real newbie question but I never worked with c / c++ iostream/fstream before and now I need to create and write a binary file.

Problem is, that I have a char *buffer, which should first write out some stuff like

byte version_number
char[255] filename
_uint32 filesize

and finally, easy to do, a copy from one file to another which could easily done by reading into the buffer and writing it afterwards.

But I'm a little lost when it comes to fill the buffer with that byte/int values since it's a char buffer...

would something like

buffer = new char[sizeof(filesize)];
&buffer = filesize;

work out?

Thanks for any replys :)

Best regards,
Koepi

Nic
20th May 2002, 14:31
Hiya Koepi....

If I understand you correctly, your trying to cast some values like:
byte version_number
char[255] filename
_uint32 filesize

into a "char *buffer " & back again?

One poor way of doing it :) is to use CopyMemory :)

i.e.
struct
{
byte version_number ;
char filename[255];
_uint32 filesize;
} KOEPISTRUCT;

KOEPISTRUCT str;
str.version_numer = 5; etc
etc
etc
char *buffer = new char[sizeof(KOEPISTRUCT)];
CopyMemory(buffer, &str, sizeof(KOEPISTRUCT)); // (you can do the reverse, by swapping the first two params...use memcpy for linux)

:D but if all you want is for buffer to point to the set of variables
KOEPISTRUCT str;
char *buf = (char*)&str;

buf will now point to the values in str.

Also why use iostream? Ive always hated it :)

That above is all messy, but thats because im not quite sure what you want :) Ask again & ill try to be more specific :)

Cheers,
-Nic

Koepi
20th May 2002, 15:11
Hi Nic,

thanks for the fast reply.

first, to explain what I need this for, it's for a small proggi to create the backup-files for XCD :)

I want to use iostream/fstream since this is basic c / c++ and should be available on all platforms.

I didn't want to mess around with memory, but by the looks of it I have to... it's always dangerous to do that.

so if memcpy is available on windows as well, I'll stick with that, as I want the internal routines to be portable - but my first variant is a MFC GUI app for this.

So it's going to be some ugly

char *buffer = new char[sizeof(backup_version)];
memcpy(buffer, &backup_version, sizeof(backup_version));
file.write(buffer, sizeof(backup_version));
char *buffer = new char[sizeof(filename)];
memcpy(buffer, &filename, sizeof(filename));
file.write(buffer, sizeof(filename));

... and so on.

I hope this works, we'll find out later on tonight ;)

Best regards and thanks a million again,

Koepi

Nic
20th May 2002, 15:17
Remember to a delete [] buffer; after all news & before you do a new again...

But infact why dont you (because theres no need to copy the data):

char *buffer;

buffer = (char *)&version_number;
file.write(buffer, sizeof(version_number));
buffer = (char *)&filename;
file.write(buffer, sizeof(filename));
etc.

or use the struct method so you just do the writing (& reading once)
i.e.
KOEPISTRUCT str;
char *buffer = (char*)&str;
file.write(buffer, sizeof(KOEPISTRUCT));

-Nic

Koepi
20th May 2002, 15:29
Hehe, thx, if that really works I'd _love_ to do it that way as it doesn't require me to mess around with the memory.

The idea with the struct is in fact a very good idea that I'll pick up.

After I finished my GUI later tonight I'll try to strip that down as a command line tool - avih wants to have those tols as rudimentary as possible. (Which isn't bad if you think of this format as modified BSD licensed and should be adopted by standalone chipset manufacturers ;) ).

Thanks for your help Nic, I really appreciate it, and when I'm over in the UK one day remind me to spend you some beers :)

Thanks,
Koepi

Nic
20th May 2002, 15:45
No probs :) My dad's just come back from Germany (frankfurt & hanover)...He's recommending hoffmeister beer to me :)

-Nic