题目: 将文本文件file1.dat的内容复制到文本文件file2.dat中,同时将文件中所有小写字母转换为大写字母
#include <stdio.h>
#include <ctype.h> // 为了使用islower和toupper函数
int main() {
FILE *file1 = fopen("file1.dat", "r"); // 打开file1.dat进行读取
if (file1 == NULL) {
perror("打开file1.dat失败");
return 1;
}
FILE *file2 = fopen("file2.dat", "w"); // 打开或创建file2.dat进行写入
if (file2 == NULL) {
fclose(file1); // 如果file2无法打开,先关闭file1
perror("打开或创建file2.dat失败");
return 1;
}
int ch; // 用于存储从file1.dat读取的字符
while ((ch = fgetc(file1)) != EOF) { // 逐个字符读取file1.dat
if (islower(ch)) { // 如果字符是小写字母
ch = toupper(ch); // 转换为大写字母
}
fputc(ch, file2); // 将处理后的字符写入file2.dat
}
fclose(file1); // 关闭file1.dat
fclose(file2); // 关闭file2.dat
return 0;
}