C/Development/error
Содержание
Define Macro for string variable
#include <stdio.h>
#if !defined( HELLO_MESSAGE )
# error "You have forgotten to define the header file name."
#endif
char *format = "%s",
*hello = HELLO_MESSAGE;
int main() {
printf ( format, hello );
}
Get pointer to error message string: how to use strerror
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
FILE *file;
file = fopen ("my.txt","r");
if (file == NULL)
printf ("Error opening file my.txt: %s\n", strerror(errno));
return 0;
}
Preprocessor: error
#include <stdio.h>
int main(void)
{
int i;
i = 1;
#error This is an error message.
printf("%d", i); /* this line will not be compiled */
return 0;
}
Preprocessor: line
#include <stdio.h>
int main(void)
{
int i;
/* reset line number to 1000 and file name to myprog.c
*/
#line 1000 "myprog.c"
#error Check the line number and file name.
return 0;
}
Print error message: how to use perror
#include <stdio.h>
int main ()
{
FILE *file;
file=fopen ("my.ent", "rb");
if (file==NULL)
perror ("An error has occurred");
else
fclose (file);
return 0;
}
Reset error indicators: writing errors
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile = fopen("f.txt","r");
if (pFile==NULL)
perror ("Error opening file");
else {
fputc ("v",pFile);
if (ferror (pFile)) {
printf ("Error Writing to f.txt\n");
clearerr (pFile);
}
fgetc (pFile);
if (!ferror (pFile))
printf ("No errors.\n");
fclose (pFile);
}
return 0;
}