sscanf problems

J

Jerith

Hey guys,
I am having a problem with stack corruption in my code (it tells me
that the stack around the variable 'code' is corrupted). I think I what
is happening is that in both instances of the sscanf that I am using,
'code' is being given more than it can handle. So my question is is
there some way I can replace sscanf with something that won't corrupt
the stack around 'code'?

Thanks in advance,
JC


void CSEPAC_Logic_BuilderDlg::OnButtonSave()
{
CFile f;
CString txt;
CString newfile;
int i;
unsigned short code;
FILE *fp;

char strFilter[] = { "SLB Files (*.slb)|*.slb|All Files (*.*)|*.*||"
};

CFileDialog
FlDlg(FALSE,".slb",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,strFilter);

if( FlDlg.DoModal() == IDOK )
{
f.Open(FlDlg.GetFileName(), CFile::modeCreate |
CFile::modeWrite );

newfile = f.GetFileName();
newfile.TrimRight(".slb");
newfile = newfile + ".bin";
fp = fopen(newfile, "wb");

CArchive ar(&f, CArchive::store);
ar << m_name << m_comment;
for (i = 1; i < 2001; i++)
{
m_ocl.SetRow(i); /* set the row we are operating on */

m_ocl.SetCol(INST); /* extract the ascii instruction string */
txt = m_ocl.GetText();
if (txt == "" ) break; /* last instruction ? */
else ar << txt;

m_ocl.SetCol(REG); /* extract the ascii register to operate
on */
txt = m_ocl.GetText();
ar << txt;

m_ocl.SetCol(OPCODE); /* extract the ascii-hex opcode */
txt = m_ocl.GetText();
ar << txt;
sscanf(txt,"%x",&code);
switch (code)
{
case 20: code = 0x7e7e; break;
default: code = swap_bytes(code);
case 0: break;
}

fwrite((const void*)&code, 1, sizeof(code), fp);

m_ocl.SetCol(OPERAND); /* extract the ascii-hex operand */
txt = m_ocl.GetText();
ar << txt;
if (txt != "") sscanf(txt, "%x", &code);
else code = 0x0000;
code = swap_bytes(code);
fwrite((const void*)&code, 1, sizeof(code), fp);
}
fclose(fp);
ar.Close();
MessageBox("IOMap File '" + newfile + "' has been created.");
}
else
return;

fclose(fp);
}
 
U

u.int.32.t

Jerith said:
Hey guys,
I am having a problem with stack corruption in my code (it tells me
that the stack around the variable 'code' is corrupted). I think I what
is happening is that in both instances of the sscanf that I am using,
'code' is being given more than it can handle. So my question is is
there some way I can replace sscanf with something that won't corrupt
the stack around 'code'?

Switch to using c++ iostreams and do stream >> code;
 
B

Bernd Strieder

Hello,
Hey guys,
I am having a problem with stack corruption in my code (it tells me
that the stack around the variable 'code' is corrupted). I think I
what is happening is that in both instances of the sscanf that I am
using, 'code' is being given more than it can handle. So my question
is is there some way I can replace sscanf with something that won't
corrupt the stack around 'code'?

Look up the man-page of scanf for the right format string for short int.

Bernd Strieder
 
D

Default User

Bernd said:
Hello,


Look up the man-page of scanf for the right format string for short
int.

Some compilers will issue diagnostics for *scanf() mismatches, but of
course aren't required to do so. That functionality may require a
higher warning level.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top