题目:
将输入的小写字符串写入磁盘文件,再将刚写入磁盘文件的内容读出并以大写字母显示在屏幕上。
源代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int main()
{
char file_content[50];
char ch;
FILE* fp;
fp = fopen("test_1.txt", "w+");
printf("请输入文件内容: ");
fgets(file_content, 50, stdin);
fputs(file_content, fp);
rewind(fp);
while ((ch = getc(fp)) != EOF)
{
putc(toupper(ch), stdout);
}
fclose(fp);
return 0;
}
演示效果:

讲解:
fgets(file_content, 50, stdin);
fputs(file_content, fp);
fgets负责将字符串存储到file_content数组中。fputs负责将file_content的字符串输出到文件当中去。
rewind(fp);
rewind负责将fp的光标移动到开头(之前file_content字符串输出到文件时,光标在文件结尾)
putc(toupper(ch), stdout);
toupper(ch)先将ch的字符转换为大写在输出到终端上。这里要明白ch和文件当中的字符不变还是小写的。toupper(ch)只是将取得ch得字符并大写并没有赋值给任何得左值。
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈

网硕互联帮助中心



评论前必须登录!
注册