C语言的编程实现问题

计算机D盘文件file 1.txt中保存一串英文字符。编程实现:将该文件中"指定的字母"若是小写则转换成大写,若为大写则转化成小写,并将转换后的所有文件复制到D盘文件file2 .txt中。要求:"指定的字母"由用户从键盘输入

#include <stdio.h>
int main() {
    FILE *fp1,*fp2;
    char ch,data;
    fp1 = fopen("file1.txt","r");
    fp2 = fopen("file2.txt","w");
    scanf("%c",&ch);
    if(ch >= 'A' && ch <= 'Z')
        ch += 32;
    while ((data = fgetc(fp1) != EOF))
    {
        if(data >= 'A' && data <='Z')
        {
            if(data + 32 == ch)
                data += 32;
        }
        fputc(data,fp2);
    }
    fclose(fp1);
    fclose(fp2);

    return 0;
}
 

分两部分:
1)打开文件读内容; 把内容写入文件
2)对于英语字符, 做大小写的转换。

两个功能在网上都有很多例子了。

#include <stdio.h>
#include <ctype.h>

int main()
{
    FILE *file1 = fopen("file1.txt", "r");
    FILE *file2 = fopen("file2.txt", "w");
    if (!file1 || !file2)
    {
        printf("failed to open file\n");
        if (file1)
            fclose(file1);
        if (file2)
            fclose(file2);
        return 1;
    }

    char c = tolower(getchar());
    char ch;
    while ((ch = fgetc(file1) != EOF))
    {
        if (tolower(ch) == c)
            ch = isupper(ch) ? tolower(ch) : toupper(ch);
        fputc(ch, file2);
    }

    fclose(file1);
    fclose(file2);
    return 0;
}