8 1) 2) 3) A S C I I 4 C I / O I / 8.1 8.1.1 1. ANSI C F I L E s t d i o. h typedef struct i n t _ f d ; i n t _ c l e f t ; i n t _ m o d e ; c h a r *_ n e x t ; char *_buff; /* /* /* /* /*
1 5 4 C FILE FILE *fp1,fp2; f p 1 f p 2 2. ANSI C FILE *fopen(char *fname,char *mode) s t d i o. h f o p e n () f n a m e f n a m e m o d e m o d e 8-1 m o d e 8-1 mode Mode r w a r b w b a b r + w + a + r b + w b + a b / / / / / / 8-1 FILE *fp; If ((fp=fopen("test.txt","w"))==null) /* printf("cannot open file \n"); N U L L N U L L s t d i o. h 0
8 155 3. ANSI C int fclose(file *stream) f c l o s e () s t r e a m f c l o s e () s t r e a m f c l o s e () 0 [ 8-1] m a i n ( ) FILE *fp; If ((fp=fopen("test.dat","rb"))==null) printf("cannot open file\n"); e x i t ( 0 ) ; / * * / if (fclose(fp)) printf("file close error!\n"); 8.1.2 1. C f g e t c f p u t c s t d i o. h int fgetc(file *stream) f g e t c ( ) E O F int fputc(int ch,file *stream) f p u t c c h fputc () E O F [ 8-2] m a i n () main( argc,argv) int argc;
1 5 6 C char *argv[]; char ch; FILE *fp; int i; i f (( f p = f o p e n ( a r g v [ 1 ]," r ")) = = N U L L ) printf("not open"); while ((ch=fgetc(fp))!=eof) p u t c h a r ( c h ); f c l o s e ( f p ); /* a r g v [ 1 ] /* m a i n () a rg c a rg v a rg c L 8-2. c L 8-2. e x e l 8-2. e x e c : \ t c > l8-2 L8-2.c a rg v [ 0 ] = " c :\ t c > l 8-2 " a rgv[1]=" L8-2.c " a rg c = 2 L8-2.c f g e t c () C E O F c : \ t c > l8-2 L8-2.c main( argc,argv) int argc; char *argv[]; char ch; FILE *fp; int i; i f (( f p = f o p e n ( a r g v [ 1 ]," r ")) = = N U L L ) printf("not open"); while ((ch=fgetc(fp))!=eof) p u t c h a r ( c h ); f c l o s e ( f p ); /* a r g v [ 1 ] /* [ 8-3] t e s t. t x t m a i n ( )
FILE fp; char ch; i f (( f p = f o p e n (" t e s t. t x t "," w ")) = = N U L L ) printf("cannot open file!\n"); while ((ch=fgetchar())!='\n') f p u t c ( c h, f p ) f c l o s e ( f p ); 8 157 /* /* / / t e s t. t x t D O S t y p e R U N I love china! D O S type t e s t. t x t c : \ t c > type test.txt I love china! 2. C s t d i o. h Char *fgets(char *str,int num,file *stream) fgets() s t r e a m n u m - 1 s t r E O F int fputs(char *str,file *stream) f p u t s () s t r 0 [ 8-4] t e s t. t x t #include <string.h> m a i n () FILE *fp; char str[128]; if ((fp=fopen("test.txt","w"))==null) /* printf("cannot open file!"); w h i l e (( s t r l e n ( g e t s ( s t r )))! = 0 ) /* f p u t s ( s t r, f p ); /* fputs("\n",fp); /*
1 5 8 C fclose(fp); /* 127 0 H e l l o! How do you do G o o d - b y e! d o s t y p e c : \ t c > type test.txt H e l l o! How do you do Good-bye! g e t s [ 8-5] t e s t 1. t x t t e s t 2. t x #include <string.h> m a i n () FILE *fp1,*fp2; char str[128]; if ((fp1=fopen("test1.txt","r"))==null) /* 1 printf("cannot open file\n"); if ((fp2=fopen("test2.txt","w"))==null) /* 2 printf("cannot open file\n"); while ((strlen(fgets(str,128,fp1)))>0) /* 0 fputs(str,fp2 ); /* 1 2 p r i n t f ("% s ", s t r ); /* f c l o s e ( f p 1 ); f c l o s e ( f p 2 );
8 159 3. s c a n f () p r i n t f () f f s c a n f () f p r i n t f ( ) int fscanf(file *stream,char *format,arg_list) int fprintf(file *stream,char *format,arg_list) s t r e a m s c a n f () p r i n t f () [ 8-6] m a i n () FILE *fp; int i; struct stu char name[15]; char num[6]; float score[2]; /* s t u d e n t ; /* if ((fp=fopen("test1.txt","w"))==null) /* printf("input data:\n"); for( i=0;i<2;i++) scanf("%s %s %f %f",student.name,student.num,&student.score[0], &student.score[1]); /* fprintf(fp,"%s %s %7.2f %7.2f\n",student.name,student.num, s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ]); /* f c l o s e ( f p ); /* if ((fp=fopen("test.txt","r"))==null) /* printf("output from file:\n"); while (fscanf(fp,"%s %s %f %f\n",student.name,student.num, & s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ])! = E O F /* printf("%s %s %7.2f %7.2f\n",student.name,student.num,
1 6 0 C student.score[0],student.score[1]); /* fclose(fp); /* input data: xiaowan j001 87.5 98.4 xiaoli j002 99.5 89.6 output from file: xiaowan j001 87.50 98.40 xiaoli j002 99.50 89.60 c :\ > type test.txt xiaowan j001 87.50 98.40 xiaoli j002 99.50 89.60 if ((fp=fopen("test1.txt","wb"))==null) /* 4. C int fread(void *buf,int size,int count,file *stream) int fwrite(void *buf,int size,int count,file *stream) fread stream count s i z e ( ) b u f fread f w r i t e () b u f ( ) c o u n t ( ) s t r e a m s i z e [ 8-7] #include "stdio.h" #include "stdlib.h" m a i n ( ) FILE *fp1;
8 161 int i; struct stu /* char name[15]; char num[6]; float score[2]; s t u d e n t ; if ((fp1=fopen("test.txt","wb"))==null) /* printf("input data:\n"); for( i=0;i<2;i++) scanf("%s %s %f %f",student.name,student.num, & s t u d e n t. s c o r e [ 0 ],& s t u d e n t. s c o r e [ 1 ]); /* fwrite(&student,sizeof(student),1,fp1); /* f c l o s e ( f p 1 ); if ((fp1=fopen("test.txt","rb"))==null) /* printf("output from file:\n"); for (i=0;i<2;i++) f r e a d (& s t u d e n t, s i z e o f ( s t u d e n t ), 1, f p 1 );/ * printf("%s %s %7.2f %7.2f\n",student.name,student.num, s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ]); /* f c l o s e ( f p 1 ); input data: xiaowan j001 87.5 98.4 xiaoli j002 99.5 89.6 output from file: xiaowan j001 87.50 98.40 xiaoli j002 99.50 89.60 C : int atoi(char *ptr) float atof(char *ptr) long int atol(char *ptr) m a t h. h
1 6 2 C s t d l i b. h [ 8-8] #include <stdlib.h> m a i n () FILE *fp1; char *temp; int i; struct stu char name[15]; /* char num[6]; /* float score[2]; s t u d e n t ; if ((fp1=fopen("test.txt","wb"))==null) for( i=0;i<2;i++) printf("input name:"); g e t s ( s t u d e n t. n a m e ); printf("input num:"); g e t s ( s t u d e n t. n u m ); printf("input score1:"); g e t s ( t e m p ); s t u d e n t. s c o r e [ 0 ] = a t o f ( t e m p ); printf("input score2:"); g e t s ( t e m p ); s t u d e n t. s c o r e [ 1 ] = a t o f ( t e m p ); /* / * /* /* /* /* f w r i t e (& s t u d e n t, s i z e o f ( s t u d e n t ), 1, f p 1 ); f c l o s e ( f p 1 ); /* if ((fp1=fopen("test.txt","rb"))==null) e x i t ( 0 ) ; p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - \ n " ) ; p r i n t f ( " % - 1 5 s % - 7 s % - 7 s % - 7 s \ n ", " n a m e ", " n u m ", " s c o r e 1 ", " s c o r e 2 " ) ; p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - \ n " ) ; for (i=0;i<2;i++) f r e a d ( & s t u d e n t, s i z e o f ( s t u d e n t ), 1, f p 1 ) ; p r i n t f ( " % - 1 5 s % - 7 s % 7. 2 f % 7. 2 f \ n ", s t u d e n t. n a m e, s t u d e n t. n u m, s t u d e n t. s c o r e [ 0 ], s t u d e n t. s c o r e [ 1 ] ) ;
8 163 f c l o s e ( f p 1 ) ; R U N input name:l i - y i n g input num: j 0 1 2 3 input score1:9 8. 6 5 input score2:8 9. 6 input name:l i - l i input num: j 0 1 2 4 input score1:6 8. 6 5 input score2:8 6. 6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - n a m e n u m s c o r e 1 s c o r e 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - l i - y i n g j 0 1 2 3 9 8. 6 5 8 9. 6 0 l i - l i j 1 2 4 6 8. 6 4 8 6. 6 0 8.1.3 C int fseek(file *fp,long d,int pos) f p d p o s P o s 0 1 2 d l o n g 0 f s e e k ( f p, 5 L, 0 ) 5 [ 8-9] f s e e k ( f p,- 10 L, 2 ) 10 rewind() ftell(file *fp) 5 #include <stdlib.h> #define n 5 m a i n ( ) FILE *fp1; /*
1 6 4 C char *temp; int i,j; struct stu char name[15]; char num[6]; float score[2]; s t u d e n t [ n ]; /* if ((fp1=fopen("test.txt","wb"))==null) for( i=0;i<n;i++) printf("input name:"); g e t s ( s t u d e n t [ i ]. n a m e ); printf("input num:"); g e t s ( s t u d e n t [ i ]. n u m ); printf("input score1:"); g e t s ( t e m p ); s t u d e n t [ i ]. s c o r e [ 0 ] = a t o f ( t e m p ); printf("input score2:"); g e t s ( t e m p ); s t u d e n t [ i ]. s c o r e [ 1 ] = a t o f ( t e m p ) ; /* /* /* /* /* fwrite(&student[i],sizeof(struct stu),1,fp1); /* fclose(fp1); /* if ((fp1=fopen("test.txt","rb+"))==null) /* p r i n t f ("- --- --- --- --- --- --- --\ n "); p r i n t f ("% - 15 s %- 7 s %- 7 s %- 7 s \ n "," n a m e "," n u m "," s c o r e 1 "," s c o r e 2 "); p r i n t f ("- --- --- --- --- --- --- --\ n "); for (i=0;i<n;i++) /* fread(&student[i],sizeof(struct stu),1,fp1); p r i n t f ("% - 15 s %- 7 s % 7. 2 f % 7. 2 f \ n ", s t u d e n t [ i ]. n a m e, s t u d e n t [ i ]. n u m, s t u d e n t [ i ]. s c o r e [ 0 ], s t u d e n t [ i ]. s c o r e [ 1 ]); /* fseek(fp1,3*sizeof(struct stu),0); /* fwrite(&student[1],sizeof(struct stu),1,fp1); /* rewind(fp1); /* p r i n t f ("- --- --- --- --- --- --- --\ n ");
8 165 p r i n t f ( " % - 1 5 s % - 7 s % - 7 s % - 7 s \ n ", " n a m e ", " n u m ", " s c o r e 1 ", " s c o r e 2 " ) ; p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - \ n " ) ; for (i=0;i<n;i++) /* fread(&student[i],sizeof(struct stu),1,fp1); p r i n t f ("% - 15 s %- 7 s % 7. 2 f % 7. 2 f \ n ", s t u d e n t [ i ]. n a m e, s t u d e n t [ i ]. n u m, s t u d e n t [ i ]. s c o r e [ 0 ], s t u d e n t [ i ]. s c o r e [ 1 ]); f c l o s e ( f p 1 ) ; /* R U N input name:l i - y i n g input num: j 0 1 2 3 input score1:9 8. 6 5 input score2:8 9. 6 input name:l i - l i input num: j 0 1 2 4 input score1:6 8. 6 5 input score2:8 6. 6 input name:l i - p i n g input num: j 0 1 2 5 input score1:8 8. 5 input score2:8 4. 6 input name:w a n g - x i a n input num: j 0 1 2 6 input score1:9 8 input score2:94 input name:m a - l i n g input num: j 0 1 2 7 input score1:6 6. 5 input score2:8 0. 6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - n a m e n u m s c o r e 1 s c o r e 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - l i - y i n g j 0 1 2 3 9 8. 6 5 8 9. 6 0 l i - l i j 0 1 2 4 6 8. 6 4 8 6. 6 0 l i - p i n g j 0 1 2 5 8 8. 5 0 8 4. 6 0 Wang-xian j0126 98.009 4. 0 0 Ma-ling j01276 6. 5 0 8 0. 6 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - n a m e n u m s c o r e 1 s c o r e 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - l i - y i n g j 0 1 2 3 9 8. 6 5 8 9. 6 0 l i - l i j 0 1 2 4 6 8. 6 4 8 6. 6 0 l i - l i j 0 1 2 4 6 8. 6 4 8 6. 6 0 Wang-xian j0126 98.009 4. 0 0
1 6 6 C M a - l i n g j 0 1 2 7 6 6. 5 0 8 0. 6 0 8.2 A N S I 1. A N S I U N I X I / O i o. h int open(char *fname,int access) f n a m e a c c e s s a c c e s s O _ R D O N LY O _ W R O N LY O _ R D W R close(int fd) U N I X #include "io.h" #include "fcntl.h" #include "sys\stat.h" m a i n ( a r g c, a r g v ) int argc; char *argv[] int fd; if((fd=open(argv[1],o_rdonly))==-1) /* printf("cannt open file!"); printf("file existent!"); if (close(fd)) printf("error in closing file\n"); 2. i o. h int read(int fd,void *buf,int count) r e a d () f d c o u n t b u f
8 167 int write(int fd,void *buf,int count) w r i t e () c o u n t b u f f d T E S T. T S T 100 b u ff e r #include "io.h" #include "stdio.h" #include "fcntl.h" m a i n () int fd; char buffer[100]; if ((fd=open("test.tst",o_rdonly))==-1) /* printf("cannot open file!\n"); if (read(fd,buffer,100)!=100) /* printf("possible read error."); 8.3 [ 8-10] a d d r. t x t t e l. t x t a d d r t e l. t x t type addr.txt h e j i e t i a n j i n g l i y i n g s h a n g h a i l i m i n g c h e n g d u w a n g p i n c h o n g q i n g type tel.txt l i y i n g 12 34 5 h e j i e 87 64 w a n g p i n 87 64 3 l i m i n g 765 432 14 1 0
1 6 8 C #include <stdlib.h> #include <conio.h> #include <string.h> m a i n ( ) FILE *fptr1,*fptr2,*fptr3; /* char temp[15],temp1[15],temp2[15]; if ((fptr1=fopen("addr.txt","r"))==null)/* if ((fptr2=fopen("tel.txt","r"))==null) if ((fptr3=fopen("addrtel.txt","w"))==null) c l r s c r (); /* while(strlen(fgets(temp1,15,fptr1))>1) /* 1 f g e t s ( t e m p 2, 15, f p t r 1 ); /* f p u t s ( t e m p 1, f p t r 3 ); /* f p u t s ( t e m p 2, f p t r 3 ); /* s t r c p y ( t e m p, t e m p 1 ); /* do /* f g e t s ( t e m p 1, 15, f p t r 2 ); f g e t s ( t e m p 2, 15, f p t r 2 ); while (strcmp(temp,temp1)!=0); r e w i n d ( f p t r 2 ); /* f p u t s ( t e m p 2, f p t r 3 ); /* f c l o s e ( f p t r 1 ); f c l o s e ( f p t r 2 ); f c l o s e ( f p t r 3 ); /*
8 169 a d d r t e l. t x t type addrtel.txt hejie tianjing 87 64 liying shanghai 12 34 5 liming chengdu 765 432 wangpin chongqing 87 64 3