C++文件流操作PTA

完善程序:从文本文件s1.txt中读入多行字符到内存,将其中的小写字母全改成大写字母,然后输出到文本文件d1.txt中。


#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(//空1//)
    {
        if((ch>='a')&&(ch<='z'))
        {
            ch=ch-32;
        }
        //空2//;
        ch=fgetc(fp1);
    }
    fclose(fp1);
    fclose(fp2);
    return 0;
}

感谢有缘人

基于new bing的编写:

#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;
    }

    while ((ch = fgetc(fp1)) != EOF) { // 读取文件,直到读到文件结束符
        if (ch >= 'a' && ch <= 'z') {
            ch -= 32; // 将小写字母转化为大写字母
        }
        fputc(ch, fp2); // 将字符写入文件
    }

    fclose(fp1);
    fclose(fp2);

    return 0;
}