We help IT Professionals succeed at work.

First-chance exception with fscanf_s

apostrophe27
apostrophe27 asked
on
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

Comment
Watch Question

jkr
BRONZE EXPERT
Top Expert 2012

Commented:
'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...

Author

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.
jkr
BRONZE EXPERT
Top Expert 2012

Commented:
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

Author

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.
BRONZE EXPERT
Top Expert 2012
Commented:
Aren't you missing the 'size' argument, i.e.


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

Open in new window

Author

Commented:
>>Aren't you missing the 'size' argument, i.e.

That's it. Thanks.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.