[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1178
  • Last Modified:

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

0
apostrophe27
Asked:
apostrophe27
  • 3
  • 3
1 Solution
 
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
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
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
 
apostrophe27Author Commented:
>>Aren't you missing the 'size' argument, i.e.

That's it. Thanks.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now