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

解析:

  1. 打开 's2.dat' 和 'd2.dat' 文件,分别用 ifstream 和 ofstream 对象 fp1 和 fp2 表示。
  2. 通过 fp1.read() 函数从 's2.dat' 文件中读取一个字符,存储在变量 ch 中。
  3. 判断 ch 是否为大写字母,如果是则将其转换为小写字母。
  4. 将处理后的字符 ch 通过 fp2.write() 函数写入 'd2.dat' 文件中。
  5. 重复 2-4 步,直到 fp1.eof() 为真,即文件已读完。
  6. 关闭 fp1 和 fp2 文件流,程序结束。
C++ 程序优化:将二进制文件中的大写字母转换为小写字母

原文地址: https://www.cveoy.top/t/topic/okAJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录