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 *