C //习题10.3 从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test“中保存,输入的字符串以“!“结束。

C程序设计 (第四版) 谭浩强 习题10.3

习题10.3 从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存,输入的字符串以"!"结束。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。
代码块
方法:使用指针,函数的模块化设计,动态分配内存
说明:这里准备先写入的文件名称为Test.txt,文件已经存在于该项目目录下。
c 复制代码
#include <stdio.h>
#include <stdlib.h>

void initialVar(char **name, char **word){
	*name = (char*)malloc(80 * sizeof(char));
	*word = (char*)malloc(sizeof(char));
}

void inputFileName(FILE **file, char *name){
	printf("Enter File Name: ");
	scanf("%s", name);

	*file = fopen(name, "r");
	if(*file == NULL){
		perror("Cannot open this file");
		system("pause");
		exit(0);
	}
}

void fileInput(FILE **file, char *name, char *word){
	*file = fopen(name, "w+");
	if(*file == NULL){
		perror("Cannot open this file");
		system("pause");
		exit(0);
	}

	printf("Enter String: ");
	while((*word = getchar()) != '!'){
		if(*word >= 'a' && *word <= 'z'){
			*word -= 32;
		}
		fputc(*word, *file);
	}
	fclose(*file);
	putchar(10);
}

void freeVar(char **name, char **word){
	free(*name);
	free(*word);
}

int main(){
	FILE *file = NULL;
	char *name = NULL;
	char *word = NULL;

	initialVar(&name, &word);
	inputFileName(&file, name);
	fileInput(&file, name, word);
	freeVar(&name, &word);

	system("pause");
	return 0;
}
运行结果如下:
相关推荐
luj_17682 小时前
塔防牌:策略与卡牌的智慧碰撞
服务器·c语言·开发语言·经验分享·算法
wuyk5552 小时前
3.链表:用指针串联的动态数据结构
c语言·开发语言·数据结构·链表
郝学胜-神的一滴3 小时前
干货版《算法导论》17:二叉树核心原理、遍历逻辑与高阶实操全解
数据结构·c++·python·算法·计算机·编程
爱编程的小新☆3 小时前
【LeetCode】从递归到 Flood Fill:5 道题吃透 DFS 的选择、回溯与标记
java·算法·leetcode·深度优先·回溯·flood fill
Water_Sunzhipeng3 小时前
2024牛客暑期多校训练营1
算法
hetao17338373 小时前
2026-08-09~08-14 hetao1733837 的刷题记录
c++·算法
技术小黑3 小时前
RNN算法实战系列06 | LSTM 实现糖尿病探索与预测
rnn·算法·lstm
Lzh编程小栈3 小时前
【嵌入式底层】SPI超透彻详解:通信原理、四线结构、四种模式、寄存器底层 + 面试全集
c语言·stm32·单片机·面试·职场和发展
疯狂打码的少年3 小时前
【数据结构】二叉树的性质(五大性质+计算)
数据结构·笔记·算法
wabs6664 小时前
关于图论【A*算法 | 卡码网127.骑士的攻击的思考】
数据结构·算法·图论·卡码网·广搜的改进版