C Primer Plus 第十三章程序清单……2015.5.15

来源:互联网 发布:js删除json数组 对象 编辑:程序博客网 时间:2024/05/18 01:26

C Primer Plus  

       第五版

第十三章  文件输入/输出


程序清单13.1
#include<stdio.h>
#include<stdlib.h> 
int main(int argc,char *argv[])
{
int ch;
FILE *fp;
long count=0;
if(argc!=2)
{
printf("Usage:%s filename\n",argv[0]);
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL)
{
printf("Cant open %s\n",argv[1]);
exit(1);
}
while((ch=getc(fp))!=EOF)
{
putc(ch,stdout);
count++;
}
fclose(fp);
printf("File %s has %ld characters\n",argv[1]);
return 0;
}
程序清单13.2


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN 40
int main (int argc,char *argv[])
{
FILE *in,*out;
int ch;
char name[LEN];
int count=0;
if(argc<2)
{
fprintf(stderr,"Usage:%s filename\n",argv[0]);
exit(1);
}
if((in=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"I couldnt open the file\"%s\"\n",argv[1]);
exit(2);
}
strcpy(name,argv[1]);
strcat(name,".red");
if((out=fopen(name,"w"))==NULL)
{
fprintf(stderr,"Cant creat output file.\n");
exit(3);
}
while((ch=getc(in))!=EOF)
if(count++%3==0)
  putc(ch,out);
if(fclose(in)!=0||fclose(out)!=0)
  fprintf(stderr,"Error in closing files\n");
  return 0;
}
程序清单13.2
#include<stdio.h>
#include<stdlib.h>
#define MAX 40
int main(void)
{
FILE*fp;
char words[MAX];
if((fp=fopen("words","a+"))==NULL)
{
fprintf(stdout,"cant open \"words\"file.\n");
exit(1);
}
puts("Enter words to add to the file;press the enter");
puts("Key at the beginning of a line to terminate.");
while(gets(words)!=NULL&&words[0]!='\0')
  fprintf(fp,"%s",words);
puts("File cotents:");
rewind(fp);//回到文件的开始初
while(fscanf(fp,"%s",words)==1)
  puts(words); 
if(fclose(fp)!=0)
     fprintf(stderr,"Error closing file\n");
return 0;
}
程序清单13.3
#include<stdio.h>
#define MAXLINE 5
int main (void)
{
char line[MAXLINE];
while(fgets(line,MAXLINE,stdin)!=NULL&&line[0]!='\n')
    fputs(line,stdout);
    return 0;
}
程序清单13.4
#include<stdio.h>
#include<stdlib.h>
#define CNTL_Z '\032'
#define SLEN 50
int main(coid)
{
char file[SLEN];
char ch;
FILE*fp;
long count,last;
puts("Enter the name of the file to be processed:");
gets(file);
if((fp=fopen(file,"rb"))==NULL)
{
printf("reverse cant open %s\n",file);
exit(1);
}
fseek(fp,0l,SEEK_END);
last=ftell(fp);
for(count=1l;count<=last;count++) 
{
fseek(fp,-count,SEEK_END);
ch=getc(fp);
if(ch!=CNTL_Z&&ch!='\r')
  putchar(ch);
if(ch=='\r')
 putchar('\n');
else
 putchar(ch);

}
putchar('\n');
fclose(fp);
return 0;

程序清单13.4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source,FILE *dest);
int main(void)
{
FILE *fa,*fs;
int files=0;
char file_app[SLEN];
char file_src[SLEN];
puts("Enter name of destonation file:");
gets(file_app);
if((fa=fopen(file_app,"a"))==NULL)
{
fputs("Cant create output buffer\n",stderr);
exit(2);
}
if(setvbuf(fa,NULL,_IOFBF,BUFSIZE)!=0)
{
fputs("Cant creat output buffer\n",stderr);
exit(3);
}
puts("Enter name of first source file (empty line to quit):");
while(gets(file_src)&&file_src[0]!='\0')
{
if(strcmp(file_src,file_app)==0)
  fputs("Cant appnd file to itself\n",stderr);
else if ((fs=fopen(file_src,"r"))==NULL)
  fprintf(stderr,"cant open %s\n",file_src);
else
 {
  if(setvbuf(fs,NULL,_IOFBF,BUFSIZE)!=0)
  {
  fputs("Cant appnd file to itself\n",stderr);
  continue;
  }
  append(fs,fa);
  if(ferror(fs)!=0)
   fprintf(stderr,"Error in resding file %s.\n",file_src);
  if(ferror(fa)!=0)
   fprintf(stderr,"Error in wrinting file %s.\n",file_app);
  fclose(fs);
  files++;
  printf("FILe %s appended.\n",file_src);
  puts("Next file ");
 }
}
printf("Done,%d files appended.\n",files);
fclose(fa);
return 0;
}
void append(FILE*source,FILE*dest)
{
size_t bytes;
static char temp[BUFSIZE];
while((bytes=fread(temp,sizeof(char),BUFSIZE,source))>0)
 fwrite(temp,sizeof(char),bytes,dest);
}
程序清单13.7
#include<stdio.h>
#include<stdlib.h>
#define ARSIZE 1000
int main(void)
{
double numbers[ARSIZE];
double value;
const char*file="numbers.dat";
int i;
long pos;
FILE *iofile;
for(i=0;i<ARSIZE;i++)
 numbers[i]=100.0*i+1.0/(i+1);
if((iofile=fopen(file,"wb"))==NULL)
{
fprintf(stderr,"count open %s for output\n",file);
exit(1);
}
fwrite(numbers,sizeof(double),ARSIZE,iofile);
fclose(iofile);
if((iofile=fopen(file,"rb"))==NULL)
{
fprintf(stderr,"could not open %s for random acess.\n",file);
exit(1);
}
printf("Enter an index in the rang %d\n",ARSIZE-1);
scanf("%di");
while(i>=0&&i<ARSIZE)
{
pos=(long)i*sizeof(double);
fseek(iofile,pos,SEEK_SET);
fread(&value,sizeof(double),1,iofile);
printf("%f\n",value);
scanf("%d",&i);
}
fclose(iofile);
puts("Bye!");
return 0;
}
0 0
原创粉丝点击