C/Development/error
Содержание
Define Macro for string variable
<source lang="cpp">
- 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 );
}
</source>
Get pointer to error message string: how to use strerror
<source lang="cpp">
- 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;
}
</source>
Preprocessor: error
<source lang="cpp">
- 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;
}
</source>
Preprocessor: line
<source lang="cpp">
- 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;
}
</source>
Print error message: how to use perror
<source lang="cpp">
- 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;
}
</source>
Reset error indicators: writing errors
<source lang="cpp">
- 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;
}
</source>