我正在从一个文件中读取内容,以便在C中读取一个char数组。我怎样才能将文件中所有小写字母改为大写字母?
答案 0 :(得分:1)
这是一种可能的算法:
以下是用C编写的代码:
#include <stdio.h>
#include <ctype.h>
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
int main()
{
// 1. Open a file
FILE *inputFile = fopen(INPUT_FILE, "rt");
if (NULL == inputFile) {
printf("ERROR: cannot open the file: %s\n", INPUT_FILE);
return -1;
}
// 2. Open another file
FILE *outputFile = fopen(OUTPUT_FILE, "wt");
if (NULL == inputFile) {
printf("ERROR: cannot open the file: %s\n", OUTPUT_FILE);
return -1;
}
// 3. Read the content of the input file
int c;
while (EOF != (c = fgetc(inputFile))) {
// 4 & 5. Capitalize and write it to the output file
fputc(toupper(c), outputFile);
}
// 6. Close all file handles
fclose(inputFile);
fclose(outputFile);
return 0;
}