By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

sscanf problems

P: n/a
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_OVERW RITEPROMPT,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);
}

Jun 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Jerith wrote:
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;

Jun 12 '06 #2

P: n/a
Hello,

Jerith wrote:
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

Jun 12 '06 #3

P: n/a
Bernd Strieder wrote:
Hello,

Jerith wrote:
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.


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
Jun 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.