Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Programming and Hacking > Development

Reply
 
Thread Tools Search this Thread Display Modes
Old 20th May 2002, 14:06   #1  |  Link
Koepi
Moderator
 
Koepi's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 4,454
using char *buffer with generic content in c?

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
Koepi is offline   Reply With Quote
Old 20th May 2002, 14:31   #2  |  Link
Nic
Moderator
 
Join Date: Oct 2001
Location: England
Posts: 3,285
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)

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
Nic is offline   Reply With Quote
Old 20th May 2002, 15:11   #3  |  Link
Koepi
Moderator
 
Koepi's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 4,454
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
Koepi is offline   Reply With Quote
Old 20th May 2002, 15:17   #4  |  Link
Nic
Moderator
 
Join Date: Oct 2001
Location: England
Posts: 3,285
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
Nic is offline   Reply With Quote
Old 20th May 2002, 15:29   #5  |  Link
Koepi
Moderator
 
Koepi's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 4,454
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
Koepi is offline   Reply With Quote
Old 20th May 2002, 15:45   #6  |  Link
Nic
Moderator
 
Join Date: Oct 2001
Location: England
Posts: 3,285
No probs My dad's just come back from Germany (frankfurt & hanover)...He's recommending hoffmeister beer to me

-Nic
Nic is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:38.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.