01-10-2007 03:51 PM
01-10-2007 04:45 PM
Use you use SetBreakOnLibraryErrors to handle the error yourself instead of having CVI pop up a error box.
int bPrevBreak;
FILE *ptr;
// ignore library errors opening a file: code will handle errors
bPrevBreak = SetBreakOnLibraryErrors (FALSE);
ptr = fopen (str_file, "r");
SetBreakOnLibraryErrors (bPrevBreak); // restore previous break condition
if(ptr == NULL) // error happen
// handle the error
else
// do something else
01-11-2007 07:44 AM
It works beautifully, by the way how can we verify the error info.?
Many thanks
01-11-2007 08:54 AM
fopen sets a global named errno. You don't have to declare errno. It's declared in errno.h which is included in ansi_c.h which you need to #include in order to use fopen().
If you have the Windows SDK installed, you can use FormatMessage to get the error message. See the example I posted here: http://forums.ni.com/ni/board/message?board.id=180&message.id=12548&query.id=112013#M12548
Here's the meat of it:
#include <ansi_c.h>
#include <windows.h>
#include <utility.h>
main()
{
char errorMsg[256];
int prevBreak;
FILE *fp;
// disable break on library errors
prevBreak = SetBreakOnLibraryErrors (0);
// try opening a non-existent file
if ((fp = fopen ("ThisFileDoesn'tExist.txt", "r")) == NULL)
{
// get the error message from the errno set by fopen
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errno,
LANG_NEUTRAL,
errorMsg,
256,
NULL);
// display the error in a messagebox
MessageBox( NULL,
errorMsg, "Error opening file",
MB_APPLMODAL | MB_ICONWARNING);
}
// restore previous break on library errors state
SetBreakOnLibraryErrors (prevBreak);
}
If you want to check the errno yourself, look at the errors defined in errno.h, then try something like this.
if ((fpOutputFile = fopen (outputFileName, "w")) == NULL)
{
switch (errno)
{
case ENOENT:
strcpy (errMsg1, "No such file or directory.");
break;
case EACCES:
strcpy (errMsg1, "Access denied. File may be already open.");
break;
case EIO:
strcpy (errMsg1, "I/O error.");
break;
case ENOMEM:
strcpy (errMsg1, "Insufficient memory.");
break;
case EMFILE:
strcpy (errMsg1, "Too many open files.");
break;
case ENOSPC:
strcpy (errMsg1, "No space left on device.");
break;
default:
sprintf(errMsg1, "Error number %d.", errno);
}
sprintf(errMsg, "Error opening file %s\n%s\n"
"Click OK to try default path.",
outputFileName, errMsg1);
MessagePopup ("File Error", errMsg);
return 0;
}
01-11-2007 02:45 PM
It is very really helpfull!
Many thanks again