Unsigned char conversion probs

Started by
3 comments, last by Nerothos 20 years, 3 months ago
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!
Jump in and crash the system...it''s quite humbling.
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...
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]
Great, thanks a lot. Sorry for the FAQable quesion.
Jump in and crash the system...it''s quite humbling.
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.

This topic is closed to new replies.

Advertisement