gpt4 book ai didi

c - 如何将文本文件中的小写字母替换为大写字母

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:39 25 4
gpt4 key购买 nike

我正在将每个小写字符转换为大写字符,但它不会在文件中进行替换。我究竟做错了什么?

#include <stdio.h>

int main() {
FILE *file;
char ch;
file = fopen("file.txt", "r+");
if (file == NULL) {
printf("Error");
exit(1);
} else {
while ((ch = fgetc(file)) != EOF) {
if (ch >= 96 && ch <= 123) {
ch = ch - 32;
putc(ch, file);
}
}
fclose(file);
return 0;
}
}

最佳答案

您必须打开另一个文件才能写入。

fileOut = fopen("fileOut.txt", "w");

ch 必须是整数。

int ch;

像这样检查 men 页面。

#man fgetc

和:

putc(ch,fileOut); 

应该在 if block 之外。

关于c - 如何将文本文件中的小写字母替换为大写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46268022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com