fgets与gets的问题
着相 2011-07-26 04:53:06 例题如下:当使用gets(file);时,运行正常,而fgets就提示不能打开文件而退出,请求解答
#include <stdio.h>
#include <stdlib.h>
#define BUF 20
int main(void)
{
char file[BUF];
int ch;
FILE *fp;
long count=0;
printf("Enter the filename: ");
fgets(file,BUF,stdin); /*当使用gets(file);时,运行正常,而fgets就提示不能打开文件而退出*/
if((fp=fopen(file,"r"))==NULL)
{
printf("Can't open %s\n",file);
system("pause");
exit(1);
}
while((ch=getc(fp))!=EOF)
{
putc(ch,stdout);
count++;
}
fclose(fp);
printf("file %s has %ld characters\n",file, count);
system("pause");
return 0;
}