This presentation is the property of its rightful owner.
Sponsored Links
1 / 78

第十章 文件 PowerPoint PPT Presentation


  • 96 Views
  • Uploaded on
  • Presentation posted in: General

第十章 文件. 本章学习重点: 文件的基本概念 文件的打开、关闭 文件的读写 文件的定位、文件的检测. 本章目录. 第二十五讲 文件概述、文件打开与关闭 第二十六讲 文件读写 第二十七讲 文件的定位和文件的检测 本章小结. 结束. 第二十五讲 文件概述、文件打开与关闭. 【思考题 10-1 】 如果我们需要将程序运行结果存放到外存时,应该用什么方法存储呢?应该怎样读入数据或存储数据呢? ( 一 ) 程序分析 我们如果要将一个字符串 "This is a C program." 存放到一个文件中,应该怎么设计程序呢?请看下例。. 返回到本章目录.

Download Presentation

第十章 文件

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


3411994


3411994


3411994

10-1

"This is a C program."


3411994

#include <stdio.h>

main()

{ FILE *fp;

fp=fopen("file1.txt","w");

fprintf(fp,"This is a C program.");

fclose(fp);

}


3411994

"file1.txt""This is a C program."fprintffscanf"file1.txt"


3411994

1

scanfprintf////


3411994

2

1

textASCII

2


3411994

3

CUNIXC

1


3411994

2

UNIX


3411994

4

1FILE

FILEFILE


3411994

2

1

2

3

1

2

3

4

FILE *;


3411994

  • fopenfclose


5 fopen

5fopen

  • ANSI C/fopen()

  • fnamemode10-1

FILE *fp;

fp=fopen(fname,mode) ;


3411994

10-1 fopen


3411994

\\\

FILE *fp;

fp=fopen("c:\\user\\abc.txt","w");

Cuser abc.txtfp


6 fclose

6fclose

fclose

fclose(fp);

fclose0EOF(-1)ferror

fclose();


3411994

10-1D"d:\abc.txt"

fopen"r"

FILE *fp;

fp=fopen("d:\\abc.txt","r");


3411994

10-2D"d:\abc.txt"D

fopen\\\"r+""w+""r+""w+"

FILE *fp;

fp=fopen("d:\\abc.txt ","r+"); /**/

fp=fopen("d:\\abc.txt ","w+"); /**/


3411994

10-3 10-1"d:\abc.txt"

fclose

fclose(fp);


3411994

fputcfgetc

fputsfgets

fwritefread

fprintffscanf


Fputc fgetc

fputcfgetc

10-2

ABCEOF"c:\file1.txt""c:\file1.txt"


3411994

#include "stdio.h"

main()

{ char a='A',b='B',c='C',filename[20];

FILE *fp;

clrscr(); /**/

printf("\nPlease input the write filename:");

scanf("%s",filename); /**/

if((fp=fopen(filename,"w"))==NULL) /**/

{ printf("write cannot open file\n");

exit(0);

}

fputc(a,fp); /*a*/


3411994

"c:\file1.txt""w"fputc()abcEOFWindowsfile1.txt4ABC


1 fputc

1fputc

fputcputc()

chfpfputc(ch,fp)chfpEOFEOFstdio.h-10xFF

fputc(ch,fp);

putc(ch,fp);


2 fgetc

2fgetc

fgetcgetc()

fpchEOF

ch=fgetc(fp);

ch=getc(fp);


Fputs fgets

fputsfgets

10-3

"Hello,""all ""the ""world ""people!""c:\file2.txt""c:\file2.txt"


3411994

#include "stdio.h"

main()

{ char a[][9]={"Hello,","all ","the ","world ","people!"};

char filename[20],out[9];

int i;

FILE *fp;

clrscr(); /**/

printf("\nPlease input the write filename:");

scanf("%s",filename); /**/

if((fp=fopen(filename,"w"))==NULL) /**/

{ printf("write cannot open file\n");

exit(0);

}


3411994

for(i=0;i<=4;i++)

fputs(a[i],fp); /**/

fclose(fp);

/**/

if((fp=fopen(filename,"r"))==NULL) /**/

{ printf("read cannot open file\n");

exit(0);

}

while((fgets(out,9,fp))!=NULL) /*fp*/

printf("%s",out);

fclose(fp); /**/

}


3411994

fputc(b,fp); /*b*/

fputc(c,fp); /*c*/

fputc(0xff,fp); /*EOF*/

fclose(fp); /**/

/**/

if((fp=fopen(filename,"r"))==NULL) /**/

{ printf("read cannot open file\n");

exit(0);

}

while((a=fgetc(fp))!=EOF) /**/

putchar(a);

fclose(fp); /**/

}


3411994

"c:\file2.txt""w""c:\file2.txt"fputs()a"c:\file2.txt"


3 fputs

3fputs()

1

2strlength00

fputs();


4 fgets

4fgets()

1

2/'\0'strlength+1EOF

fgets(/+1);


Fwrite fread

fwritefread

10-4

freadfwrite


3411994

#include "stdio.h"

#include "string.h"

struct student /**/

{ int num; /**/

char name[20]; /**/

int chinese,math,english; /**/

};

main()

{ FILE *fp;

int i;

char ch,filename[30];

float f;

struct student stu[30],st; /**/

clrscr(); /**/

printf("\nPlease input the write filename:");


3411994

scanf("%s",filename); /**/

if((fp=fopen(filename,"wb"))==NULL) /**/

{ printf("Cannot open %s file\n",filename);

exit(0);

}

/*nN*/

i=0;

do

{ printf("please input information of the %d student:",i+1);

printf("\nnumber name chinese math english\n");

scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].chinese,

&stu[i].math,&stu[i].english);

fwrite(&stu[i],sizeof(stu[i]),1,fp);

i++;


3411994

printf("Have another student record(y/n)?");

getchar();

ch=getchar();

}while(ch=='y'||ch=='Y'); /*yY*/

fclose(fp);

/**/

if((fp=fopen(filename,"rb"))==NULL) /**/

{ printf("read cannot open file %s!\n",filename);

exit(0);

}

printf("\nInformation of the student score:");

printf("\n number name chinese math english");

while(fread(&st,sizeof(st),1,fp)==1)


3411994

printf("\n%5d%7s%5d%5d%5d",st.num,st.name,st.chinese,st.math, st.english);

if(!feof(fp))

printf("\n file read error");

fclose(fp);

}

"c:\file3.txt""wb""c:\file3.txt"fwrite()"c:\file3.txt"yYnN"rb"fread()


3411994


5 fwrite

5fwrite

1fwrite

2fwritebufsizecountfp

fwrite(buf,size,count,fp);


3411994

3bufcountfpsizesize*countcountfwrite


6 fread

6fread

1fread

2bufsizecountfpfpsizecountbufsize*countfreadcount

fwrite(buf,size,count,fp);


Fprintf fscanf

fprintffscanf

10-5

fprintffscanf


3411994

#include "stdio.h"

struct student

{ char name[10];

int num;

int age;

char addr[15];

}boy[2],*bp;

main()

{ FILE *fp;

char ch,filename[20];

int i;

clrscr(); /**/

printf("\nPlease input the write filename:");

gets(filename); /**/

bp=boy;


3411994

if((fp=fopen(filename,"wb+"))==NULL)

{ printf("Cannot open file!");

getch();

exit(1);

}

printf ("input 2 boy information:");

printf("\nname num age addr:\n");

for(i=0;i<2;i++,bp++)

scanf("%s%d%d%s",bp->name,&bp->num,&bp->age,bp->addr);

bp=boy;

for(i=0;i<2;i++,bp++) /**/

fprintf(fp,"%s %d %d %s\n",bp->name,bp->num,bp->age,bp->addr);

fclose(fp);


3411994

/**/

if((fp=fopen(filename,"r"))==NULL) /**/

{ printf("read cannot open file %s!\n",filename);

exit(0);

}

bp=boy; /*bp*/

for(i=0;i<2;i++,bp++) /**/

fscanf(fp,"%s %d %d %s\n",bp->name,&bp->num, &bp->age,bp->addr);

printf("name\t number age addr\n");

bp=boy;

for(i=0;i<2;i++,bp++)

printf("%s\t%5d%10d%12s\n",bp->name,bp->num,bp->age,bp->addr);

fclose(fp);

}


3411994


7 fprintf

7fprintf

fprintfprintf

&

fprintf();


8 fscanf

8fscanf

  • fscanfscanf

  • &&

fscanf();


3411994

9

1feof()

fpfp10

2/putwgetwC/getw

feof(fp);

=getw();


3411994

putw

putw(10,fp);

10fp

i=getw(fp);

i

putw(,);


3411994

10-4"c:\upper.txt""!""c:\upper.txt"

#include "stdio.h"

main()

{ FILE *fp;

char str[100],filename[10];

int i=0;


3411994

if((fp=fopen("c:\\upper.txt","w"))==NULL)

{ printf("Can not open file\n");

exit(0);

}

printf("Enter a string:\n");

gets(str);

while(str[i]!='!')

{ if(str[i]>='a'&&str[i]<='z')

str[i]=str[i]-32;

fputc(str[i],fp);

i++;

}


3411994

fclose(fp);

fp=fopen("c:\upper.txt","r");

fgets(str,strlen(str)+1,fp);

printf("%s\n",str);

fclose(fp);

}


3411994


3411994

10-610-4"c:\file3.txt"

fopen()"rb"


3411994

#include "stdio.h"

#include "string.h"

struct student /**/

{ int num /**/

char name[20]; /**/

int chinese,math,english; /**/

};

main()

{ FILE *fp;

int i;

char ch,filename[30];

float f;

struct student stu[30]; /**/


3411994

clrscr();

printf("\nPlease input the write filename:");

scanf("%s",filename); /**/

if((fp=fopen(filename,"rb"))==NULL) /**/

{ printf("Cannot open %s file\n",filename);

exit(0);

}

printf("\nInformation of the student score:");

printf("\n number name chinese math english\n");


3411994

for(i=0;i<3;i+=2)

{ fseek(fp,i*sizeof(struct student),0); /**/

fread(&stu[i],sizeof(struct student),1,fp);/*i*/

printf("\n%5d%9s%7d%9d%7d",stu[i].num,stu[i].name,stu[i].chinese,

stu[i].math,stu[i].english);

}

fclose(fp);

}


3411994

"c:\file3.txt"


1 rewind

1rewind

1

2rewind

rewind();


2 ftell

2ftell

1

2ftell

3ftellftell-1L

ftell();


3 fseek

3fseek

1

2fseek00

fseek(,,);


3411994

012

10-2C

long64KB


3411994

10-7

fopen()"rb"ferrorclearerr


3411994

#include "stdio.h"

main()

{ char c,filename[30];

FILE *fp;

clrscr(); /**/

printf("\nPlease input the open filename:");

scanf("%s",filename); /**/

fp=fopen(filename,"r");

if(ferror(fp)) /**/


3411994

{ printf("Error reading from %s file\n",filename);

clearerr(fp); /*0*/

exit(0);

}

else

{ while ((c=fgetc(fp))!=EOF)/**/

putchar(c);

fclose(fp); /**/

}

}


3411994


3411994

C/feof()ferror()0clearerr()


4 feof

4feof()

1

2feof()10

int feof();


5 ferror

5ferror()

//fputcfgetcfreadfwriteferror

1

2ferror()fp//10

int ferror();


6 0 clearerr

60clearerr()

1

2clearer()0/ferrorclearerr(fp)ferror(fp)0

int clearer();


3411994

7


3411994

10-5"c:\member.dat""c:\member.dat"<>

#include "stdio.h"

main()

{ FILE *fp;

int flag;

char name[20],data[20];


3411994

if((fp=fopen("c:\\member.dat","a+"))==NULL) /**/

{ printf("Open file error\n");

exit(0);

}

do

{ printf("Enter name(end with input empty):\n");

gets(name); /**/

if(strlen(name)==0) /**/

break;

strcat(name,"\n"); /**/

rewind(fp); /**/

flag=1;


3411994

while(flag&&(fgets(data,30,fp)!=NULL))

/**/

if(strcmp(data,name)==0) /**/

flag=0;

if(flag)

fputs(name,fp);

else

printf("\nThis name has been existed!\n");

}while(ferror(fp)==0); /**/

fclose(fp);

}


3411994


3411994

  • /


3411994

  • /


  • Login