#include
#include
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.read(&ch, sizeof(ch));
while(!fp1.eof())
{
if((ch>='A')&&(ch<='Z'))
{
ch=ch+32;
}
fp2.write(&ch, sizeof(ch));
fp1.read(&ch, sizeof(ch));
}
fp1.close();
fp2.close();
return 0;
}
解析:
- 打开 's2.dat' 和 'd2.dat' 文件,分别用 ifstream 和 ofstream 对象 fp1 和 fp2 表示。
- 通过 fp1.read() 函数从 's2.dat' 文件中读取一个字符,存储在变量 ch 中。
- 判断 ch 是否为大写字母,如果是则将其转换为小写字母。
- 将处理后的字符 ch 通过 fp2.write() 函数写入 'd2.dat' 文件中。
- 重复 2-4 步,直到 fp1.eof() 为真,即文件已读完。
- 关闭 fp1 和 fp2 文件流,程序结束。