c语言的,把小写转大写,哪错了

2025-02-24 14:46:22
推荐回答(4个)
回答1:

1、main函数中不应该调用fopen函数打开filename文件,后面调用函数中再打开的时候,会导致重复打开同一个文件;
2、fprintf(stderr,"...")使用错误,stderr没有定义,建议修改为printf或puts输出;
3、copy_like中fopen的模式要用"r"和"w",不用用"rb"和"wb",因为下面用的getc和putc;
4、对所有字符都执行-32是错误的,应判断是否是小写字母,小写字母-32
程序修改如下:
#include
#include
int copy_like(char*oldname,char*newname);
int main()
{
char filename[20];
char destination[20];
printf("Enter filename");
scanf("%s",filename);
printf("Enter the destination");
scanf("%s",destination);
if((copy_like(filename,destination))==0)
puts("Successful");
else
puts("Error2"); //stderr 没定义
return 0;
}
int copy_like(char*oldname,char*newname)
{
FILE *oldname_file;
FILE *newname_file;
char x,y;
if((oldname_file=fopen(oldname,"r"))==NULL)
{
puts("Error3");
return -1;
}
if((newname_file=fopen(newname,"w"))==NULL)
{
puts("Error4");
return -1;
}
while(!feof(oldname_file)) //文件是否结束
{
x=getc(oldname_file);
if ( (x>='a')&&(x<='z') ) //是否是小写字母
y=x-32;
else
y = x;
fputc(y,newname_file);
}
fclose(oldname_file);
fclose(newname_file);
return 0;
}

回答2:

第一:主函数里定义的fp没有用上,可以去掉
第二:取到X之后,应该先判断X的值确实是小写字母,再过y=x-32 ,否则y=x

回答3:

if(feof(oldname_file)==0) 改成:if(feof(oldname_file)!=0)

y=x-32; 也不确切, 应该判断 x 的值是否在 97-122 之间。
if(x>=97 && x<=122)
y=x-32;
else
y=x;

回答4:

#include