C++ Code to Convert Lowercase to Uppercase in a File
The code attempts to open two files, 's1.txt' for reading and 'd1.txt' for writing. It then reads each character from 's1.txt', checks if it is a lowercase letter, converts it to uppercase, and writes it to 'd1.txt'.
However, there is a mistake in the line ch=fputc(fp2);. The fputc function takes two arguments - the character to be written and the file pointer to the output file. In this line, only the file pointer is passed as an argument. The correct line would be fputc(ch, fp2);
Here is the corrected code:
#include <iostream>
using namespace std;
int main()
{
FILE *fp1,*fp2;
char ch;
if ((fp1=fopen("s1.txt","r"))==NULL)
{
cout<<"can not open s1.txt\n";
return 0;
}
if ((fp2=fopen("d1.txt","w"))==NULL)
{
cout<<"can not open d1.txt\n";
return 0;
}
ch=fgetc(fp1);
while( ch!= EOF )
{
if((ch>='a')&&(ch<='z'))
{
ch=ch-32;
}
fputc(ch, fp2);
ch=fgetc(fp1);
}
fclose(fp1);
fclose(fp2);
return 0;
}
原文地址: http://www.cveoy.top/t/topic/oljg 著作权归作者所有。请勿转载和采集!