c primer plus第十三章编程练习答案(纯手打欢迎指错)

本文提供了C Primer Plus第十三章的编程练习答案,详细解答了包括13.1、13.2和13.11等题目,涵盖C语言输入输出方面的实践操作。
摘要由CSDN通过智能技术生成

13.1

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
	int ch;
	FILE *fp;
	long count =0;
	char file[100];
	printf("Please enter the name of file.\n");
	scanf("%s",file);
	if((fp=fopen(file,"r"))==NULL)
	{
		fprintf(stderr,"Can't open the file.\n");
		exit(1);
	}
	while((ch=getc(fp))!=EOF)
	{
		putc(ch,stdout);
		count++;
	}
	fclose(fp);
	printf("\nFile %s has %ld characters\n",file,count);
	return 0;
}

13.2

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
	char ch;
	if(argc!=3)
	{
		fprintf(stderr,"Error!");
		exit(1);
	}
	FILE*fp;
	FILE*fp2;
	if((fp=fopen(argv[1],"rb"))==NULL)
	{
		fprintf(stderr,"Can t open the file %s",argv[1]);
		exit(1);
	}
	if((fp2=fopen(argv[2],"wb"))==NULL)
	{
		fprintf(stderr,"Can t open the file %s",argv[2]);
		exit(1);
	}
	while((ch=getc(fp))!=EOF)
	{
		
		putc(ch,fp2);
	}
	fclose(fp);
	fclose(fp2);
	
	return 0;
	
} 
13.3

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(void)
{
	FILE*fp;
	FILE*fp2;
	char ch[100];
	char ch2[100];
	char c;
	printf("Please enter source file name.\n");
	scanf("%s",ch);
	printf("Please enter target file name.\n");
	scanf("%s",ch2);
	if((fp=fopen(ch,"r"))==NULL)
	{
		fprintf(stderr,"Can't open the file %s.\n",ch);
		exit(1);
	}
	if((fp2=fopen(ch2,"w"))==NULL)
	{
		fprintf(stderr,"Can't open the file %s.\n",ch2);
		exit(1);
	}
	while((c=getc(fp))!=EOF)
	{
		c=toupper(c);
		putc(c,fp2);
	}
	fclose(fp);
	fclose(fp2);
	return 0;
}

13.4

#include<stdio.h>
#include<stdlib.h>
int main(int argc,int *argv[])
{
	FILE*fp;
	char ch;
	for(int i=1;i<argc+1;i++)
	{
		if((fp=fopen(argv[i],"r"))==NULL)
		{
			fprintf(stderr,"Can't open the file ",argv[i]);
			exit(1);
		}
		while((ch=getc(fp))!=EOF)
		{
			putc(ch,stdout);
		}
		puts(" "); 
		fclose(fp);
	}
	return 0;
	
}

13.5

#include<stdio.h>// 文件名后的第一个命令提示符是目标文件,后面的的命令提示符可以是源文件。
#include<stdlib.h>
#include<string.h>
#define BUFSIZE 1024
#define SLEN 81
void append(FILE*source,FILE*dest);

int main(int argc,char *
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值