1

2
10.1 C
,
ASCIIASCII

3
int10000
0010011100010000
0010011100010000
0011000100110000001100000011000000110000
ASCII

4
a

5
FILE
FILE
FILEstdio.h
typedef struct
{ int _fd; //
int _cleft; //
int _mode; //
char *_next; //
char *_buff; //
}FILE;
10.2

6
FILE *fp;
,
C

7
C,stdio.h
:-->/-->
------ stdin
------ stdout
----- stderr
fopen
FILE *fopen(char *name,char *mode)
NULL
FILE *fp;
fp=fopen(“aa.c”,“w”);
if(fp==NULL)
{ printf(“File open error!\n”);
exit(0);
}
FILE *fp;
fp= fopen (“c:\\fengyi\\bkc\\test.dat”,”r”);
FILE *fp;
char *filename=“c:\\fengyi\\bkc\\test.dat”
fp= fopen(filename,”r”);
“r+/rb+” ()
“a/ab” ()
“w/wb” ()
“r/rb” ()
“w+/wb+” ()
“a+/ab+” ()
/
/
//
//
//
/
10.3

8
,
int fclose(FILE *fp)
fp
0;,0
a
fclose
fclose

9
I/O:fputcfgetc
fputc
int fputc(int c,FILE *fp)
cfp
c;EOF
fgetc
int fgetc(FILE *fp)
fp;EOF
#define putc(ch,fp) fputc(ch,fp)
#define getc(fp) fgetc(fp)
#define putchar( c ) fputc(c,stdout)
#define getchar( ) fgetc(stdin)
10.4

10
while(!feof(fp))
{ c=fgetc(fp);
……..
}
#include <stdio.h>
main()
{ FILE *fp;
char ch,*filename=“out.txt”;
if((fp=fopen(filename,"w"))==NULL)
{ printf("cannot open file\n");
exit(0);
}
printf("Please input string:");
ch=getchar();
while(ch!='#')
{ fputc(ch,fp);
putchar(ch);
ch=getchar();
}
fclose(fp);
}
#include <stdio.h>
main()
{ FILE *fp;
char ch,*filename=“out.txt”;
if((fp=fopen(filename,”r"))==NULL)
{ printf("cannot open file\n");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
putchar(ch);
fclose(fp);
}
feof
int feof(FILE *fp)
00
#include <stdio.h>
main()
{ FILE *in,*out;
char ch,infile[10],outfile[10];
scanf("%s",infile);
scanf("%s",outfile);
if ((in = fopen(infile,"r"))== NULL)
{ printf("Cannot open infile.\n");
exit(0);
}
if ((out = fopen(outfile,"w"))== NULL)
{ printf("Cannot open outfile.\n");
exit(0);
}
while (!feof(in))
fputc(fgetc(in),out);
fclose(in); fclose(out);
}
#“

11
size_t fread(void *buffer,size_t size,size_t count,FILE *fp)
size_t fwrite(void *buffer,size_t size,size_t count,FILE *fp)
/
/0
typedef unsigned size_t;
buffer,/
size,/
count,/
fp,/
fread fwrite /
I/O,freadfwrite

12
float f[2];
FILE *fp;
fp=fopen(“aa.dat”,“rb”);
fread(f,4,2,fp);
for(i=0;i<2;i++)
fread(&f[i],4,1,fp);
struct student
{ int num;
char name[20];
char sex;
int age;
float score[3];
}stud[10];
for(i=0;i<10;i++)
fread(&stud[i],sizeof(struct student),1,fp);

13
#include <stdio.h>
#define SIZE 2
struct student_type
{ char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
main()
{
int i;
for(i=0;i<SIZE;i++)
scanf("%s%d%d%s",stud[i].name,&stud[i].num,
&stud[i].age,stud[i].addr);
save();
display();
}
void save()
{ FILE *fp;
int i;
if((fp=fopen("d:\\fengyi\\exe\\stu_dat","wb"))==NULL)
{ printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void display()
{ FILE *fp;
int i;
if((fp=fopen("d:\\fengyi\\exe\\stu_dat","rb"))==NULL)
{ printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i +)
{ fread(&stud[i],sizeof(struct student_type),1,fp);
printf("%-10s %4d %4d %-15s\n",stud[i].name,
stud[i].num,stud[i].age,stud[i].addr);
}
fclose(fp);
}
4

14
I/O:fprintffscanf
int fprintf(FILE *fp,const char *format[,argument,…])
int fscanf(FILE *fp,const char *format[,address,…])
I/O
,I/O;,EOF
fprintf(fp,“%d,%6.2f”,i,t); //it%d,%6.2ffp
fscanf(fp,“%d,%f”,&i,&t); //3,4.5,3i,4.5t
#include <stdio.h>
main()
{ char s[80],c[80];
int a,b;
FILE *fp;
if((fp=fopen("test","w"))==NULL)
{ puts("can't open file"); exit() ; }
fscanf(stdin,"%s%d",s,&a);/*read from keaboard*/
fprintf(fp,"%s %d",s,a);/*write to file*/
fclose(fp);
if((fp=fopen("test","r"))==NULL)
{ puts("can't open file"); exit(); }
fscanf(fp,"%s%d",c,&b);/*read from file*/
fprintf(stdout,"%s %d",c,b);/*print to screen*/
fclose(fp);
}

15
char *fgets(char *s,int n,FILE *fp)
int fputs(char *s,FILE *fp)
fp/
fgets NULL
fputs EOF’
#include<stdio.h>
main()
{ FILE *fp;
char string[81];
if((fp=fopen("file.txt","w"))==NULL)
{ printf("cann't open file");exit(0); }
while(strlen(gets(string))>0)
{ fputs(string,fp);
fputs("\n",fp);
}
fclose(fp);
if((fp=fopen("file.txt","r"))==NULL)
{ printf("cann't open file");exit(0); }
while(fgets(string,81,fp)!=NULL)
fputs(string,stdout);
fclose(fp);
}
I/O,fgets fputs

16
-----
~
~
rewind
void rewind(FILE *fp)
#include <stdio.h>
main()
{ FILE *fp1,*fp2;
fp1=fopen("d:\\fengyi\\bkc\\ch12_4.c","r");
fp2=fopen("d:\\fengyi\\bkc\\ch12_41.c","w");
while(!feof(fp1)) putchar(getc(fp1));
rewind(fp1);
while(!feof(fp1)) putc(getc(fp1),fp2);
fclose(fp1);
fclose(fp2);
}
10.5

17
int fseek(FILE *fp,long offset,int whence)
00
,)
>0
<0
SEEK_SET 0
SEEK_CUR 1
SEEK_END 2
fseek(fp,100L,0);
fseek(fp,50L,1);
fseek(fp,-10L,2);
ftell
long ftell(FILE *fp)
()
-1L
313
main()
{ int i;
FILE *fp;
if((fp=fopen("studat","rb"))==NULL)
{ printf("can't open file\n");exit(0); }
for(i=0;i<3;i+=2)
{ fseek(fp,i*sizeof(struct student_type),0);
fread(&stud[i],sizeof(struct student_type),1,fp);
printf("%s %d %d %s\n",
stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
fclose(fp);
}
#include <stdio.h>
struct student_type
{ int num;
char name[10];
int age;
char addr[15];
}stud[3];
#include"stdio.h"
main()
{ FILE *fp;
char filename[80];
long length;
gets(filename);
fp=fopen(filename,"rb");
if(fp==NULL)
printf("file not found!\n");
else
{ fseek(fp,0L,SEEK_END);
length=ftell(fp);
printf("Length of File is %1d bytes\n",length);
fclose(fp);
}
}
(ch12_101.c)
fseek

18
ferror
int ferror(FILE *fp)
00
ferror
fopenferror0
10.6

19
void clearerr(FILE *fp)
0
clearerr(fp)rewind
ferror()clearerr()
#include <stdio.h>
int main(void)
{ FILE *stream;
stream = fopen("DUMMY.FIL","w");
getc(stream);
if (ferror(stream))
{ printf("Error reading from DUMMY.FIL\n");
clearerr(stream);
}
if(!ferror(stream))
printf("Error indicator cleared!");
fclose(stream);
return 0;
}
clearerr