開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Lubuntu + gcc
問題(Question):
這是我從書上照抄的程式,想練習錯誤處理
不過不知道為何compile後會有warning
ferror.c:13:16: warning: initialization makes pointer from integer without
a cast
char *es = strerror(errno);
^
還有執行後會有Segmentation fault
餵入的資料(Input):
一個不存在的file nofile.xxx
預期的正確結果(Expected Output):
無Segmentation fault
錯誤結果(Wrong Output):
Segmentation fault
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(void) {
FILE *fp;
puts("Open a file nofile.xxx");
fp = fopen("nofile.xxx","r");
if (!fp) {
perror("Error!");
}
char *es = strerror(errno);
puts(es);
clearerr(stdin);
return 0;
}
補充說明(Supplement):
程式出處:邊學邊做C語言