(V
isu
al
C+
+
6.
0
9
9
(V
isu
al
C+
+
6.
0
9.2
9.3
9.5
9.4
9.1
9.6
(V
isu
al
C+
+
6.
0 9.1.2 C
9.1.3
9.1.1 C
9.1 C
9.1.1 C
(V
isu
al
C+
+
6.
0
1,C1
2,C2
3,C3
9.1.1 C
(V
isu
al
C+
+
6.
0
9.1file.txt
l9_1.cpp
1) #include "stdio.h"
#include <stdlib.h>
void main()
{
char ch; /*ch*/
FILE *fp; /*fp*/
fp=fopen("file.txt","r");/*file*/
if(fp==NULL) /**/
{ printf("cannot open this file\n");
exit(0);
}
9.1.1 C
(V
isu
al
C+
+
6.
0
ch=fgetc(fp); /*ch*/
while(ch!=EOF) /**/
{
putchar(ch); /**/
ch=fgetc(fp); /*ch*/
}
fclose(fp); /**/
}
file,
9.1.1 C
(V
isu
al
C+
+
6.
0
file
9.1.1 C
(V
isu
al
C+
+
6.
0
9.2user.x
l9_2.cpp
#include "stdio.h"
#include <stdlib.h>
struct userinfo /**/
{ char username[10];
char password[12];
}stu;
void main()
{ FILE *fp; /*fp*/
fp=fopen("user.x","wb"); /*user.x*/
if(fp==NULL) /**/
{ printf("cannot open this file\n");
exit(0);
}
printf("Please input your username:");
scanf("%s",&stu.username);
printf("\nPlease input your password:");
scanf("%s",&stu.password);
fwrite(&stu,sizeof(userinfo),1,fp); /**/
fclose(fp); /**/
}
Please input your username:student
Please input your password:12345678
user.x
student” 12345678”
9.1.1 C
(V
isu
al
C+
+
6.
0
9.3user.x
l9_3.cpp
#include "stdio.h"
#include <stdlib.h>
struct userinfo /**/
{
char username[10];
char password[12];
}stu;
9.1.1 C
(V
isu
al
C+
+
6.
0
void main()
{ FILE *fp; /*fp*/
fp=fopen(“user.x”,“rb”); /*user.x*/
if(fp==NULL)
{ printf("cannot open this file\n");
exit(0);
}
fread(&stu,sizeof(userinfo),1,fp); /**/
printf("your username:%s\n",stu.username);
printf("your password:%s\n",stu.password);
fclose(fp); /**/
}
9.1.1 C
(V
isu
al
C+
+
6.
0
user.x2
your username:student
your password:12345678
9.1.2 C
(V
isu
al
C+
+
6.
0
CASC ASC ASC
ASC
9.1.2 C
(V
isu
al
C+
+
6.
0
2
00000100 11010010
ASC ASC
9.1.3
(V
isu
al
C+
+
6.
0
CFILEFILEstdio.h
FILE,
FILE * ;
FILE *fp;
fp FILEfpfp
(V
isu
al
C+
+
6.
0
9.2.2 fclose()
9.2.1 fopen()
9.2
9.2.1 fopen()
(V
isu
al
C+
+
6.
0
Cfopenfopen
fopen,,,);
fopen
9.2.1 fopen()
(V
isu
al
C+
+
6.
0
9.2.1 fopen()
(V
isu
al
C+
+
6.
0

“,,);
= =NULL)
{ printf(“cannot open this file\n”);
exit(0);
}
9.2.2 fclose()
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
9.3.2
9.3.1
9.3
9.3.4
9.3.3
9.3.5
9.3.1
(V
isu
al
C+
+
6.
0
fgetc()
=fgetc();
fgetcEOF 1
ch=fgetc(fp);
chfpfgetcfpch
fgetc()
9.3.1
(V
isu
al
C+
+
6.
0
fputc()
fputc(ch
chASCIIEOF fputc(“a”,fp);
afp
9.3.2
(V
isu
al
C+
+
6.
0
fgets()
fgetcfgets

9.3.2
(V
isu
al
C+
+
6.
0
9.4fgetsfile.txt
l9_4.cpp
#include,stdio.h“
#include,stdlib.h“
main()
{ char ch[50]; /**/
FILE *fp; /*fp*/
fp=fopen(”file.txt”,“r”); /*file*/
if(fp==NULL) /**/
{ printf(“cannot open this file\n”);
exit(0);
}
fgets(ch,50,fp); /**/
printf("%s",ch);
fclose(fp);
}
9.3.2
(V
isu
al
C+
+
6.
0
fputs()
fputs
fputs(“student”,fp);

fread
fread(buffer,size,count,)
buffer
size
count size
fread countsizebuffer
freadstu
9.3.3
(V
isu
al
C+
+
6.
0
fread()
9.3.3
(V
isu
al
C+
+
6.
0
fwrite()
fwrite
fwrite(buffer,size,count,)
fwritebuffercountsize
9.5 tudent.stu
l9_5.cpp
#include,stdio.h“
#include <stdlib.h>
struct student /**/
{ char sname[8]; /**/
char ssex[2]; /**/
int sage; /**/
}stu1[3],stu2[3];
void main()
{ int i;
FILE *fp; /**/
fp=fopen(“student.stu”,“wb”); /**/
if(fp==NULL) /**/
{ printf(“!”);
exit(0);
}
9.3.3
(V
isu
al
C+
+
6.
0
9.3.3
(V
isu
al
C+
+
6.
0
fread(&stu2,sizeof(student),3,fp); /**/
printf(“:\n”);
for(i=0;i<3;i++)
{ printf(“%s %s %d\n”,stu2[i].sname,stu2[i].ssex,stu2[i].sage);
}
fclose(fp); /* */
}
9.3.3
(V
isu
al
C+
+
6.
0
student.stusizeof()
9.3.4
(V
isu
al
C+
+
6.
0
fscanf()
fscanf scanfscanffscanffscanf
fscanf(,“,)
,scanf
fscanf
,”
9.3.4
(V
isu
al
C+
+
6.
0
fprintf()
fprintfprintffprintf
fprintf(,“,)
,printf
fprintf
fprintf(fp,“%d%d”,100,200);
100200fp
fprintffscanfASCIIfprintffscanffreadfwrite
9.3.5
(V
isu
al
C+
+
6.
0
getw()
getw getcgetcgetwgetw
getw( )
fprintf EOF
x=getw(fp);
fpx
9.3.5
(V
isu
al
C+
+
6.
0
putw()
putwputcputw
putw(,)
putw
EOF
putw(10,fp);
10fp
9.3.5
(V
isu
al
C+
+
6.
0
9.6
l9_6.cpp
#include,stdio.h“
#include <stdlib.h>
main()
{ int x,num;
FILE *fp; /**/
fp=fopen(”file.txt”,“wb”); /**/
printf(“:”);
scanf(“%d”,&num);
putw(num,fp); /**/
fclose(fp); /**/
fp=fopen(“file.txt”,“rb”); /**/
x=getw(fp); /**/
printf(“\n:%d\n”,x);
fclose(fp); /**/
}
(V
isu
al
C+
+
6.
0
9.4.2
9.4.1
9.4
9.4.1
(V
isu
al
C+
+
6.
0
rewind()
rewind
rewind()
rewind
C
9.7
l9_7.cpp
#include,stdio.h“
#include <stdlib.h>
main()
{ int x,num;
FILE *fp; /**/
fp=fopen(”file.txt”,“wb+”); /**/
printf(“:”);
scanf(“%d”,&num);
putw(num,fp); /**/
rewind(fp); /**/
x=getw(fp); /**/
printf("\n:%d\n",x);
fclose(fp);
}
9.4.1
(V
isu
al
C+
+
6.
0
fseek
fseek(,)
,L”
012
9.4.1
(V
isu
al
C+
+
6.
0
fseek()
0
9.4.1
(V
isu
al
C+
+
6.
0
fseek(fp,20L,0)
20
fseek(fp,50L,1)
50
fseek(fp,-80L,2)
80
fseek
l9_8.cpp
#include,stdio.h“
#include <stdlib.h>
struct student /**/
{ char sname[8];
char ssex[2];
int sage;
}stu;
void main()
{
FILE *fp; /**/
fp=fopen("student.stu","rb");
if(fp==NULL)
{ printf("cannot open the file student.stu!");
exit(0);
}
9.4.1
(V
isu
al
C+
+
6.
0
9.4.1
(V
isu
al
C+
+
6.
0
,”
fseekfreed
fseek
9.4.2
(V
isu
al
C+
+
6.
0
ftell
ftell()
ftell-1L
ftell()
9.4.2
(V
isu
al
C+
+
6.
0
feof()
feof()
feof( );
feof0
#include,stdio.h“
#include <stdlib.h>
struct student /**/
{ char sname[8];
char ssex[2];
int sage;
}stu;
void main()
{ FILE *fp; /**/
fp=fopen(“student.stu”,“rb”); /**/
printf(“%ld\n”,ftell(fp));
fseek(fp,sizeof(student),0); /**/
printf(“%ld\n”,ftell(fp));
printf("%d\n",feof(fp));
fclose(fp); /**/
}
9.4.2
(V
isu
al
C+
+
6.
0
9.4.2
(V
isu
al
C+
+
6.
0
0fseek16feof0
(V
isu
al
C+
+
6.
0
9.5.2 clearerr()
9.5.1 ferror()
9.5
ferror()
ferror0
ferrorferror
FILE *fp;
fp=fopen(“,,);
if(ferror(fp))
printf(“!”);
9.5.1 ferror()
(V
isu
al
C+
+
6.
0
#include,stdio.h“
void main()
{ char c;
FILE *fp;
fp=fopen(”file.txt”,“w”); /**/
printf("");
9.5.2 clearerr()
(V
isu
al
C+
+
6.
0
clearerr()
clearerr0
0clearerrrewind
0
32
00
000
9.5.2 clearerr()
(V
isu
al
C+
+
6.
0
printf(“%d\n”,ferror(fp)); /**/
c=fgetc(fp); /**/
printf(“\n );
printf(“%d\n”,ferror(fp)); /**/
clearerr(fp); /*0*/
printf(“\n0 );
printf(“%d\n”,ferror(fp)); /*0*/
fclose(fp);
}
9.6
(V
isu
al
C+
+
6.
0
1
2
3
4
#include <stdio.h>
#include <stdlib.h>
struct clsset /**/
{ char class12[6]; /*1-2*/
char class34[6]; /*3-4*/
char class56[6]; /*5-6*/
}cls[5],cls2;
void save() /*,*/
{ FILE *fp;
int i;
fp=fopen("class","wb");
if(fp==NULL)
{ printf("cannot open the file.");
exit(0);
}
9.6
(V
isu
al
C+
+
6.
0
printf("\n");
for(i=0;i<5;i++)
{ scanf("%s%s%s",&cls[i].class12,&cls[i].class34,&cls[i].class56);
fwrite(&cls[i],sizeof(struct clsset),1,fp);
}
fclose(fp);
}
void somedaycls() /*,*/
{ FILE *fp;
int no;
fp=fopen("class","rb");
if(fp==NULL)
{ printf("cannot open the file.");
exit(0);
}
9.6
(V
isu
al
C+
+
6.
0
printf("(1,2,3,4,5):");
scanf("%d",&no);
if(no>=1 && no<=5)
{ fseek(fp,(no-1)*sizeof(clsset),0);
fread(&cls2,sizeof(clsset),1,fp);
printf("\n%s,%s,%s\n",cls2.class12,cls2.class34,cls2.class56);
fclose(fp);
}
else
printf("1--5!\n");
}
9.6
(V
isu
al
C+
+
6.
0
void clssheet() /**/
{ FILE *fp;
int i;
fp=fopen("class","rb");
if(fp==NULL)
{ printf("cannot open the file.");
exit(0);
}
for(i=0;i<5;i++)
{ fread(&cls[i],sizeof(clsset),1,fp);
printf("%s %s %s\n",cls[i].class12,cls[i].class34,cls[i].class56);
}
fclose(fp);
}
9.6
(V
isu
al
C+
+
6.
0
void main()
{ int op;
printf("1.\n");
printf("2.\n");
printf("3.\n");
printf("4.\n");
printf("(1--4):");
scanf("%d",&op);
switch(op)
{ case 1:save();break;
case 2:somedaycls();break;
case 3:clssheet();break;
case 4:printf("!");break;
default:printf("!");
}
}
9.6
(V
isu
al
C+
+
6.
0