C primer plus 编程练习 13.11

本文详细解读C Primer Plus书籍中的第13.11章节编程练习,涵盖关键概念和解题思路,帮助读者深入理解C语言指针和数组的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//cpp 13.11-1
#include <stdio.h>
#include <stdlib.h>
#define LEN 41

int main(void)
{
	int ch;
	FILE *fp;
	char file[LEN];
	int count = 0;

	puts("请输入您要打开的文件名:");
	scanf("%40s",file);
	if ((fp = fopen(file,"r")) == NULL)
	{
		printf("无法打开%s\n",file);
		exit(EXIT_FAILURE);
	}
	while ((ch = getc(fp)) != EOF)
	{
		putc(ch,stdout);
		count++;
	}
	fclose(fp);
	printf("文件 %s 含有 %d 个字符\n",file,count);

	return 0;
}
//CPP 13.11-3
#include <stdlib.h>
#include <ctype.h>
#define LEN 41

int main()
{
    int ch; //读取文件时存储每个字符的地方
    FILE * fp1;
	FILE * fp2;
	char file[LEN];
    puts("请输入您要打开的文件名:");
	scanf("%40s",file);
	if ((fp1 = fopen(file,"r")) == NULL)
	{
		printf("无法打开%s\n",file);
		exit(EXIT_FAILURE);
	}
	if ((fp2 = fopen(file,"a")) == NULL)
	{
		printf("无法打开%s\n",file);
		exit(EXIT_FAILURE);
	}
	while ((ch = getc(fp1)) != EOF)
	{
		ch = toupper(ch);
		putc(ch,fp2);
	}
    if (fclose(fp1) != 0)
        printf("Could not close file %s\n", file);
	if (fclose(fp2) != 0)
        printf("Could not close file %s\n", file);
    return 0;
}
//CPP 13.11-6
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 80

int main(void)
{
	FILE *in,*out;
	int ch;
	char nameout[LEN];
    char namein[LEN];
	int count = 0;
    //打开源文件,设置输入
	puts("请输入您要打开的文件名:");
	scanf("%s",namein);
	if ((in = fopen(namein,"r")) == NULL)
	{
		printf("无法打开%s\n",namein);
		exit(EXIT_FAILURE);
	}
    //设置输出
    strncpy(nameout,namein,LEN - 5); //拷贝文件名
    nameout[LEN - 5] = '\0';
	strcat(nameout,".red");
	if ((out = fopen(nameout,"w")) == NULL)
	{
		fprintf(stderr,"无法打开创建的输出文件\n");
		exit(3);
	}
	//拷贝数据
	while ((ch = getc(in)) != EOF)
	{
		if (count++ % 3 == 0)
		    putc(ch,out);      //只输出三个字符中的第一个
	}
	if (fclose(in) != 0 || fclose(out) != 0)
	{
		fprintf(stderr,"关闭文件时失败");
	}
	return 0;
}
//CPP 13.11-8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 80

int main(int argc,char *argv [])
{
	FILE *fp;
	char ch;   //待统计的字符
	char chf;
	int count = 0;   //统计计数
	char input[100];
	if (argc <= 2)
	{   
		if (argc == 1)
		{
		    printf("您没输入待统计的字符,请输入\n");
	        scanf("%c",&ch);
		}
		printf("您没有输入待统计的文件名,请输入一段待统计的字符\n");
		scanf("%100s",input);
		puts(input);
		int i = 0;
		while ((chf = input[i]) != '\0' )
	    {
			if (ch == chf)
			    count++;
			i++;
	    }
		printf("您输入的字符串中含有 %d 个 %c 字符\n",count,ch);
	}
	else
	{
		if ((fp = fopen(argv[2],"r")) == NULL)
		{
            fprintf(stderr,"无法打开%s\n",argv[2]);
		    exit(EXIT_FAILURE);
		}
		ch = argv[1][0];
		while ((chf = getc(fp)) != EOF)
	    {
			if (ch == chf)
			    count++;
	    }
		printf("文件 %s 中含有 %d 个 %c 字符\n",argv[2],count,ch);
	    if (fclose(fp) != 0)
		    fprintf(stderr,"关闭文件时失败");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值