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;
}
运行结果如下:
相关推荐
晚霞的不甘4 小时前
Flutter for OpenHarmony 可视化教学:A* 寻路算法的交互式演示
人工智能·算法·flutter·架构·开源·音视频
望舒5134 小时前
代码随想录day25,回溯算法part4
java·数据结构·算法·leetcode
C++ 老炮儿的技术栈5 小时前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
KYGALYX5 小时前
逻辑回归详解
算法·机器学习·逻辑回归
铉铉这波能秀5 小时前
LeetCode Hot100数据结构背景知识之集合(Set)Python2026新版
数据结构·python·算法·leetcode·哈希算法
踢足球09295 小时前
寒假打卡:2026-2-8
数据结构·算法
IT猿手5 小时前
基于强化学习的多算子差分进化路径规划算法QSMODE的机器人路径规划问题研究,提供MATLAB代码
算法·matlab·机器人
千逐-沐风5 小时前
SMU-ACM2026冬训周报3rd
算法
wangjialelele5 小时前
Linux下的IO操作以及ext系列文件系统
linux·运维·服务器·c语言·c++·个人开发
铉铉这波能秀5 小时前
LeetCode Hot100数据结构背景知识之元组(Tuple)Python2026新版
数据结构·python·算法·leetcode·元组·tuple