First-chance exception with fscanf_s

When I use fscanf_s to read from a file I'm getting First-chance exception errors when I run the program in debug mode. If I use fscanf, I don't get an error.

Here are the errors:

First-chance exception at 0x1023beef (msvcr80d.dll) in WinGPIO.exe: 0xC0000005: Access violation writing location 0x00130000.
First-chance exception at 0xfdfdfdfd in WinGPIO.exe: 0xC0000005: Access violation reading location 0xfdfdfdfd.(this line repeats)
char instr[100];
FILE *reg_file;
 
 
if (fopen_s(&reg_file, ini_file, "r" ))
	{	
	strMessage.Format("ini file not found");
    AfxMessageBox(strMessage,MB_OK,0);
	return(false);
	}
	
	
fscanf_s(reg_file,"%s",&instr);

Open in new window

apostrophe27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
'First-chance exception in xxx...' just means that a function from within the 'xxx' caused an access-violation exception that was handled successfully inside the SEH frame that was active when the exception occurred. You can think of it being the same as if you use code like this:

long l;

__try // set up current SEH frame
{
CopyMemory ( &l, 0, sizeof ( long)); // read from 0x00000000
}
__except( EXCEPTION_EXECUTE_HANDLER) // handler for current frame
{
puts ( "We knew that this would go wrong...");
}

So let's hope that the MS progrmmer knew what they were doing ;-)

(Additional info: MS KB Article Q105675)


The article can be found at http://support.microsoft.com/support/kb/articles/q105/6/75.asp 

A first chance exception is called so as it is passed to a debugger before the application 'sees' it. This is done by sending a 'EXCEPTION_DEBUG_EVENT' to the debugger, which can now decide whether it is passed to the apllication to handle it or 'ignore' it (e.g. like an 'EXCEPTION_BREAKPOINT' aka 'int 3'). If the exception isn't handled, it becomes a '2nd chance' exception, the debugger 'sees' it the 2nd time and will usually terminate the program (without using a debugger, these exceptions end up at 'UnhandledExceptionFilter()' which will also signal the exception to the user with one of these 'nice' message boxes and terminate the program, also...)


In short: This message is only generated by a debugger & you can safely ignore it...
0
apostrophe27Author Commented:
Unfortunately, the program locks up at that point. I have to select Stop Debugging to exit. If I select Break All, it still hangs, and I have to kill it through Task Manager.
0
jkrCommented:
Um, simple problem - that has to be
fscanf_s(reg_file,"%s",instr); // no '&' needed
 
// 'instr' is already a pointer, if you want to use '&',
// that would have to be '&instr[0]'

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

apostrophe27Author Commented:
>>fscanf_s(reg_file,"%s",instr); // no '&' needed

Right. I missed that. But, it still hangs at the same spot with it changed.

I just tried fscanf instead of fscanf_s, and it works. I can continue working on on the rest of the program, but I'd like to figure out what's wrong with fscanf_s.
0
jkrCommented:
Aren't you missing the 'size' argument, i.e.


char instr[100];
 
//...
 
fscanf_s(reg_file,"%s",instr,100); 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
apostrophe27Author Commented:
>>Aren't you missing the 'size' argument, i.e.

That's it. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.