编写一个Linux C程序,打开一个文本文件,在当前目录下保存一个文件的副本,然后把此文件中的所有小写字母转换为大写字母,其他字符不变(比如原来内容为“hello 123”,则转换为“HELLO 123”)。
代码如下:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define INPUT_FILE "1.txt"
#define OUTPUT_FILE "1.1.txt"
#define OUTPUT_FILE2 "2.txt"
int main(void)
{
char c;
FILE *fin, *fout, *fout2;
fin = fopen(INPUT_FILE, "r");
if (!fin) {
perror(INPUT_FILE);
exit(1);
}
fout = fopen(OUTPUT_FILE, "w");
if (!fout) {
perror(OUTPUT_FILE);
exit(2);
}
fout2 = fopen(OUTPUT_FILE2, "w");
if (!fout) {
perror(OUTPUT_FILE2);
exit(2);
}
while ((c = fgetc(fin)) != EOF) {
fputc(c, fout);
if (islower(c))
c ^= 0x20;
fputc(c, fout2);
}
return 0;
}