Click here to Skip to main content
15,886,724 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: RegOpenKeyEx() api issue. Pin
Richard MacCutchan7-Sep-09 2:43
mveRichard MacCutchan7-Sep-09 2:43 
AnswerRe: RegOpenKeyEx() api issue. Pin
krmed8-Sep-09 0:52
krmed8-Sep-09 0:52 
QuestionBlinking cursor Pin
susanne17-Sep-09 1:58
susanne17-Sep-09 1:58 
AnswerRe: Blinking cursor Pin
Cedric Moonen7-Sep-09 3:18
Cedric Moonen7-Sep-09 3:18 
GeneralRe: Blinking cursor Pin
susanne17-Sep-09 3:34
susanne17-Sep-09 3:34 
GeneralRe: Blinking cursor Pin
Iain Clarke, Warrior Programmer7-Sep-09 4:32
Iain Clarke, Warrior Programmer7-Sep-09 4:32 
GeneralRe: Blinking cursor Pin
susanne18-Sep-09 23:38
susanne18-Sep-09 23:38 
Questionbyte alignment issue? Writing struct to file in binary mode. [modified] Pin
Sauce!7-Sep-09 1:57
Sauce!7-Sep-09 1:57 
I'm trying to write the contents of a struct to a file in binary mode, and I'm not quite getting the output I'm expecting.

the struct looks more or less like this;

struct myStruct
{
	char c[2];
	unsigned int i1;
	unsigned short s1;
	unsigned short s2;
	unsigned int i2;
	//etc.
};


Then I'm filling the struct with the following data...

myStruct temp = {{'A', 'B'}, 0x46, 0x0, 0x0, 0x36};
Finally, I write it to a file...

std::string path = "C:/";

std::ofstream file(path.c_str(), std::ios::out | std::ios::binary);
if(!file)
{
	std::cout << "Unable to open file \"" << path << "\" for writing.\n";
	return false;
}
file.write(reinterpret_cast<char *>(&temp), sizeof(myStruct));


The file writes successfully but I don't get the output I'm expecting (in hex: 4142 4600 0000 0000 0000 0000 0000 3600 0000). Instead I am getting 4100 4200 4600 0000 0000 0000 3600 0000 2800 (hex)

Now, I have a feeling this is something to do with the byte alignment of the struct, so I tried changing the struct's declaration to

#pragma pack(push)	/* push current alignment to stack */
#pragma pack(1)		/* set alignment to 1 byte boundary */

struct myStruct
{
	char c[2];
	unsigned int i1;
	unsigned short s1;
	unsigned short s2;
	unsigned int i2;
	//etc.
};

#pragma pack(pop)	 /* restore original alignment from stack */


but that didn't work.

I also tried this:

__declspec(align(1)) struct myStruct
{
	char c[2];
	unsigned int i1;
	unsigned short s1;
	unsigned short s2;
	unsigned int i2;
	//etc.
};


but that didn't have any effect either. What is going on here and how can I fix it?

modified on Monday, September 7, 2009 9:54 AM

AnswerRe: byte alignment issue? Writing struct to file in binary mode. Pin
CPallini7-Sep-09 2:24
mveCPallini7-Sep-09 2:24 
AnswerRe: byte alignment issue? Writing struct to file in binary mode. Pin
Richard MacCutchan7-Sep-09 2:39
mveRichard MacCutchan7-Sep-09 2:39 
GeneralRe: byte alignment issue? Writing struct to file in binary mode. [modified] Pin
Sauce!7-Sep-09 3:50
Sauce!7-Sep-09 3:50 
GeneralRe: byte alignment issue? Writing struct to file in binary mode. Pin
Richard MacCutchan7-Sep-09 4:57
mveRichard MacCutchan7-Sep-09 4:57 
AnswerRe: byte alignment issue? Writing struct to file in binary mode. Pin
Luc Pattyn7-Sep-09 2:39
sitebuilderLuc Pattyn7-Sep-09 2:39 
GeneralRe: byte alignment issue? Writing struct to file in binary mode. [modified] Pin
Sauce!7-Sep-09 6:04
Sauce!7-Sep-09 6:04 
GeneralRe: byte alignment issue? Writing struct to file in binary mode. Pin
Luc Pattyn7-Sep-09 6:16
sitebuilderLuc Pattyn7-Sep-09 6:16 
GeneralRe: byte alignment issue? Writing struct to file in binary mode. Pin
Richard MacCutchan7-Sep-09 10:59
mveRichard MacCutchan7-Sep-09 10:59 
QuestionStrings in EXE Pin
choramale_vs7-Sep-09 1:45
choramale_vs7-Sep-09 1:45 
AnswerRe: Strings in EXE Pin
Richard MacCutchan7-Sep-09 1:52
mveRichard MacCutchan7-Sep-09 1:52 
GeneralRe: Strings in EXE Pin
choramale_vs7-Sep-09 2:26
choramale_vs7-Sep-09 2:26 
GeneralRe: Strings in EXE Pin
Richard MacCutchan7-Sep-09 2:30
mveRichard MacCutchan7-Sep-09 2:30 
GeneralRe: Strings in EXE Pin
choramale_vs7-Sep-09 2:51
choramale_vs7-Sep-09 2:51 
GeneralRe: Strings in EXE Pin
Richard MacCutchan7-Sep-09 4:21
mveRichard MacCutchan7-Sep-09 4:21 
AnswerRe: Strings in EXE Pin
CPallini7-Sep-09 1:55
mveCPallini7-Sep-09 1:55 
GeneralRe: Strings in EXE Pin
choramale_vs7-Sep-09 3:50
choramale_vs7-Sep-09 3:50 
GeneralRe: Strings in EXE Pin
Michael Schubert7-Sep-09 4:03
Michael Schubert7-Sep-09 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.