C&;中的小写字符到大写字符;写入文件
我正在从一个文件中读取内容,以便读入C中的字符数组。如何将文件中所有小写字母更改为大写字母?以下是一种可能的算法:C&;中的小写字符到大写字符;写入文件,c,character,uppercase,lowercase,C,Character,Uppercase,Lowercase,我正在从一个文件中读取内容,以便读入C中的字符数组。如何将文件中所有小写字母更改为大写字母?以下是一种可能的算法: 打开一个文件(我们称之为-fopen() 打开另一个要写入的文件(我们称之为B)-fopen() 阅读A-getc()或fread()的内容;随便你怎么想 使您阅读的内容为大写-toupper() 将4步的结果写入B-fwrite()或fputc()或fprintf() 关闭所有文件句柄-fclose() 以下是用C编写的代码: #include <stdio.h> #
#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;
}
#包括
#包括
#定义输入文件“INPUT.txt”
#定义输出文件“OUTPUT.txt”
int main()
{
//1.打开一个文件
FILE*inputFile=fopen(输入文件,“rt”);
if(NULL==inputFile){
printf(“错误:无法打开文件:%s\n”,输入\u文件);
返回-1;
}
//2.打开另一个文件
FILE*outputFile=fopen(输出文件,“wt”);
if(NULL==inputFile){
printf(“错误:无法打开文件:%s\n”,输出文件);
返回-1;
}
//3.读取输入文件的内容
INTC;
而(EOF!=(c=fgetc(inputFile))){
//4&5.大写并将其写入输出文件
fputc(toupper(c),输出文件);
}
//6.关闭所有文件句柄
fclose(输入文件);
fclose(输出文件);
返回0;
}
ctype.h
有toupper
。到目前为止,你在哪里?你取得了什么成就?例如,您能够复制文件内容吗?我已经能够解释这些信件并写入单独的文件,但我不确定如何覆盖文档。回答得好,并且很好地使用了int c
而不是char c
。然而OP并没有表现出太多的工作值得这个好答案。谢谢你们的评论!非常感谢!它成功了,我组织得很好,我很感激。我的荣幸!玩得高兴