题目要求:
从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件test中保存,输人的字符串以"!”结束
1.实现程序:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{FILE* fp = fopen("test.txt", "w");char ch;if (fp == NULL){printf("打开文件失败");return 1;}printf("请输入字符串\n");while (1){scanf("%c", &ch);if (ch == '!')break;if (ch >= 'a' && ch <= 'z')ch = ch - 32;fprintf(fp, "%c", ch);}fclose(fp);fp = NULL;return 0;
}
2.检验程序:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{FILE* fp = fopen("test.txt", "r");char ch;if (fp == NULL){printf("打开文件失败");return 1;}printf("字符串\n");while (1){fscanf(fp, "%c", &ch);if (ch == 'K')break;printf("%c", ch);}fclose(fp);fp = NULL;return 0;
}