1、源码
#include<stdio.h>
main(){
FILE *fp;
char str[11];
if((fp=fopen("d:\\jrzh\\example\\string","rt"))==NULL){
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
}
fgets(str,11,fp);
printf("\n%s\n",str);
fclose(fp);
}
编译:
[root@localhost C]# gcc -o fgets fgets.c
fgets.c: In function 'main':
fgets.c:8: warning: incompatible implicit declaration of built-in function 'exit'
/tmp/ccmZM2Va.o: In function `main':
fgets.c:(.text+0x40): undefined reference to `getch'
collect2: ld returned 1 exit status
解决办法:
在文件中添加库文件:#include<stdlib.h>
编译: gcc -o fgets fgets.c -I/usr/include -L/usr/lib -lncurses