C++文件流操作PTA

完善程序:程序从二进制文件s2.dat中读入所有字符到内存,将其中的大写字母全改成小写字母,然后输出到二进制文件d2.dat中。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char ch;
    ifstream fp1("s2.dat",ios_base::binary);
    if (!(fp1.is_open()))
    {
        cout<<"can not open s2.dat\n";
        return 0;
    }
    ofstream fp2("d2.dat",ios_base::binary);
    if (!(fp2.is_open()))
    {
        cout<<"can not open d2.dat\n";
        return 0;
    }
fp1//空1//
;
    while(!(fp1.eof()))
    {
        if((ch>='A')&&(ch<='Z'))
        {
            ch=ch+32;
        }        
fp2//空2//
;        
fp1//空3//
;        
    }
    fp1.close();
    fp2.close();
    return 0;
}

使用了 ifstream 和 ofstream 分别打开输入文件 "s2.dat" 和输出文件 "d2.dat",并进行错误检查。然后,我们使用 fp1.read() 读取每个字符,并进行大小写转换,将转换后的字符使用 fp2.write() 写入输出文件 "d2.dat"。

请确保在运行程序之前,将输入文件 "s2.dat" 放置在程序可访问的位置,并且输出文件 "d2.dat" 可以被创建和写入:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char ch;
    ifstream fp1("s2.dat", ios_base::binary);
    if (!fp1.is_open())
    {
        cout << "Can not open s2.dat" << endl;
        return 0;
    }

    ofstream fp2("d2.dat", ios_base::binary);
    if (!fp2.is_open())
    {
        cout << "Can not open d2.dat" << endl;
        return 0;
    }

    while (fp1.read((char*)&ch, sizeof(ch)))
    {
        if (ch >= 'A' && ch <= 'Z')
        {
            ch = ch + 32;
        }
        fp2.write((char*)&ch, sizeof(ch));
    }

    fp1.close();
    fp2.close();

    return 0;
}