Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nerothos

Unsigned char conversion probs

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having problems with this Targa writing function - i''m trying to convert it from C to C++ (using the fstream class). However, I cannot figure out how to give tga.write() the necessary info without losing any data! Since fwrite takes a void pointer, it automatically converts it without losing data, but write takes a string. I know the answer is right there, but I can''t figure it out! Here is the function:
int WriteTGA(char *file, short int w, short int h, unsigned char *data) {
	unsigned char bs;
	short int ss;
	unsigned char imgt;
	int cm;
	unsigned char cs;
	unsigned char bd;
	int is;
	ofstream tga(file, (ios::out | ios::binary));
	if(!tga.is_open()) {
		tga.close();
		return 0;
	}
	imgt = 2;
	bd = 24;
	cm = 3;
	bs = 0;
	ss = 0;
	tga.write(&bs, sizeof(unsigned char));
	tga.write(&bs, sizeof(unsigned char));
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
	tga.write(&imgt, sizeof(unsigned char));
	tga.write((char *) ss, sizeof(short int));
	tga.write((char *) ss, sizeof(short int));
//	fwrite(&imgt, sizeof(unsigned char), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
	tga.write(&bs, sizeof(unsigned char));
	tga.write((char *) ss, sizeof(short int));
	tga.write((char *) ss, sizeof(short int));
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
	tga.write((char *) w, sizeof(short int));
	tga.write((char *) h, sizeof(short int));
//	fwrite(&w, sizeof(short int), 1, filePtr);
//	fwrite(&h, sizeof(short int), 1, filePtr);
	tga.write(&bd, sizeof(unsigned char));
	tga.write(&bs, sizeof(unsigned char));
//	fwrite(&bd, sizeof(unsigned char), 1, filePtr);
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
	is = (w * h * cm);
	for(int idx = 0; (idx < is); idx += cm) {
		cs = data[idx];
		data[idx] = data[idx + 2];
		data[idx + 2] = cs;
	}
	tga.write(data, (sizeof(unsigned char) * is));
	tga.close();
	return 1;
}
 
Extreme thanks!

Share this post


Link to post
Share on other sites
Advertisement
std::ostream::write does NOT take a string! It takes a char *. There is a world of difference. C interprets null-terminated character arrays as strings in certain instances; this does not mean that character arrays are strings to C. They are simply arrays - sequences that can be addressed - and pointers to their first element iterate in steps the size of a single char.

What this means is that std::ostream::write takes a pointer to a sequence of elements of size char, which is usually a byte. ie, it''s a pointer to a stream of bytes. The second argument is simply the number of bytes (or char-sized elements, to be pedantic) to write to the output stream. So when writing a data structure to a stream, the first argument would be the address of the structure, cast to char *, while the second argument would be sizeof(<the_structure>.

In some cases, however, your compiler may "pad" a structure for better alignment. You may need to switch padding off (#pragma pack on MSVC; consult your compiler docs) to avoid sizing inconsistencies.

Man, we need a Wiki. This is such an FAQ...

Share this post


Link to post
Share on other sites
In case you didn't get it from Oluseyi's post, the problem is simply that you cast the integer values to char pointers, instead of casting the address of the integer to a char pointer. Change all the '(char*) variable' to '(char*) &variable' and it should work.

Enigma

EDIT: typo×3

[edited by - Enigma on February 5, 2004 6:28:47 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Nerothos
Great, thanks a lot. Sorry for the FAQable quesion.
It''s not your fault. We need to find a better way to present the collective knowledge of the community - searchable, extensible - so that relatively common issues like these can be addressed easily and rapidly. It''d also make for some compelling reading, and be widely available, both of which can only be good things.

The staff and mods are mulling over the possibility of a Wiki at this time. We''ll keep y''all posted.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!