C基础(C语言中的错误处理)
# include
# include
jmp _ buf ebuf;
int func();
int main(){
int I;
printf(" 1111 \ n ");
I = setjmp(ebuf);
printf("%d\n ",I);
if(I = = 0){
func();
printf("这不会被打印");
}
if(I = = 3){
printf(" 3333 \ n ");
}
printf("%d\n ",I);
返回0;
}
int func(){
printf(" 2222 \ n ");
longjmp(ebuf,3);
}
输出结果:
1111
0
2222
3
333
3
相当于一个goto语句,func
考试中重要注意事项:longjmp(dbuf,val)中val的值不能为0。如果为0,当i=setjmp(ebuf)时,系统将默认为1。
以下函数实现多个函数之间的跳转,其中具体代码在动态调用链接库
# include
# include
jmp _ bufe buf;
int jump 1();
int jump 2();
int I;
main(){
I = setjmp(ebuf);
if(I = = 0 | I = = 2){
jump 1();
}
if(I = = 1){
jump 2();
}
}
int jump 1(){
while(1){
h instance h instance;
void(* func)();
h instance = LoadLibrary(" my . dll ");
showGUI();
char s[10];
scanf("%s ",& s);
func =(void(*))GetProcAddress(h instance,s);
如果(!func){
longjmp(ebuf,1);
}
(* func)();
继续;
}
}
int jump 2(){
printf("您的输入有误\ n ");
longjmp(ebuf,2);
}
int showGUI(){
FILE * log in;
char c;
login = fopen("login.txt "," r ");
如果(!log in){
printf(" file err:log in \ n ");
退货;
}
while(1){
c = fgetc(log in);
if(c = = EOF){
break;
}
printf("%c ",c);
}
fclose(登录);
返回0;
}
0条评论