文件操作有关
这是从键盘输入一个字符串,将小写字母转换为大写字母然后在保持到名文test.txt的文件中。。。这个代码最后操作只能将字符串的第一个字母转换为大写。。。我找了很久也不知道问题出在哪。。求大神帮忙啊!!#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
FILE *fp;
char str,filename;
int i=0;
if((fp=fopen("test.txt","w"))==NULL)
{
printf("cannot open this file!\n");
exit(0);
}
printf("please enter the string and end with !:");
gets(str);
while(str!='!')
{
if(str>='a'&&str<='z')
{
str=str-32;
fputc(str,fp);
i++;
}
fclose(fp);
if((fp=fopen("test.txt","r"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
return 0;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
char str,filename;
int i=0;
if((fp=fopen("test.txt","w"))==NULL)
{
printf("cannot open this file!\n");
exit(0);
}
printf("please enter the string and end with !:");
gets(str);
while(str!='!')
{
if(str>='a'&&str<='z')
{
str=str-32;
fputc(str,fp);
}
i++;
}
fclose(fp);
if((fp=fopen("test.txt","r"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
return 0;
} 你的while循环搞那么长,文件都关闭了还怎么转换。反大括号搞到i++;后面就行了 牛头的主人 发表于 2014-1-20 23:14 static/image/common/back.gif
你的while循环搞那么长,文件都关闭了还怎么转换。反大括号搞到i++;后面就行了
十分感谢啊!!真的是这样也!!我太弱智了。。哎 Jane爱蓝黑墨水 发表于 2014-1-21 09:23 static/image/common/back.gif
十分感谢啊!!真的是这样也!!我太弱智了。。哎
出错是好事,最怕没有错
页:
[1]