C读取文件问题
从键盘上输入一个字符串,把该字符串中的小写字母转换为大写字母,输出到文件text.txt中,然后从该文件读出字符串并显示出来。
#include<stdio.h>
int main()
{
FILE *fp,*fp2;
char str[100];
int i=0;
if((fp=fopen("c:\\text.txt","w"))==NULL)
{
printf("can't open this file.\n");
exit(0);
}
printf("input a string:\n");
gets(str);
while(str[i])
{
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
fputc(str[i],fp);
i++;
}
fclose(fp);
fp2=fopen("c:\\test.txt","r");
fgets(str,100,fp2);
printf("%s\n",str);
fclose(fp2);
getch();
return 0;
}
为什么会出错呢?请大大指教指教