,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,hapter Seven,*,Click to edit Master title style,Chapter Seven,File,Chapter Seven File,1,Contents,Introduction,1,Open/close file,2,Read/write file,3,Check the state of the opening file,4,The location of the file pointer,5,4,hapter Seven,Contents Introduction1Open/clo,2,How to get the data while run a program?,Where will the result in?,How can input the result of a program to another program?,Introduction,The procedure of C language,Edit,Link,Create the program text,Compile,Compile it successfully,Link it to the C function library,hapter Seven,How to get the data while run,3,Introduction,program,format output,character output,string output,initialization,assignment,input from keyboard,memory,memory,format input,character input,string input,Question,Read data from a file,Write data to a file,hapter Seven,Introductionprogram format o,4,Introduction,File is,the gather of related information saved in memory device(assistant memory),OS manages system in files,Processing files means file input/out processing.,Procedure,1 open file,2 file read/write,3 close file.,Interface input/out devices are treated as files in C.,File accessing methods:,1)Buffered I/O(or stream I/O),2)Unbuffered I/O(or low-level I/O),hapter Seven,Introduction File is th,5,Introduction,Buffered (,temporary storage location,),Well discuss Buffered I/O in our lecture,In buffered I/O,-open a communication area,in RAM,D,I,S,k,P,R,O,G,R,A,M,Output file buffer,Input file buffer,There are two ways to deal with file:,Binary file,ASCII file,stream,hapter Seven,IntroductionBuffered,6,Introduction,stream,Stream is the abstract of file used to deal with file.,There are two kinds of streams in C.,The character is ASCII code.one ASCII code occupies one byte,new line n is end of line.After read/write a character from/to ASCII file,the fp increases automatically to guarantee the read/write sequences.,Text stream,Binary stream,Storing data to disk in the form the same as in RAM.,Example:10000,In binary file,use 2 bytes,In ASCII file,use 5 bytes,hapter Seven,Introductionstream Stream is,7,Open/close file,How to magange a file in C?,Pointer,Operations,open file,(,load file,),operate file using functions,close file,(,remove file from memory,),B,D,F,A,C,E,A file system,creat a new file,delete an old one,read/write,access a file by its name,manage the memory file used,limit the users right,hapter Seven,Open/close fileHow to magange,8,Open/close file,File pointer,points to a structure that contains information about the file,such as the,location of a buffer,the current character position in the buffer,whether the file is being,read or written,and whether errors or end of file have occurred.Users dont need to know the details,because the definitions obtained from,include a structure declaration called,FILE.,About file pointer,I am a student.,hapter Seven,Open/close file File po,9,2 The function of opening a file:,FILE*fp;,uppercase letters,otherwise,cant be recognized,defined to standard structure,key-word,#include is necessary,fopen(filename,*mode);,string,char array,char pointer,paths are allowed,1 files pointer:,opening a file use function,fopen(),read,write,append,Open/close file,hapter Seven,2 The function of opening a,10,Open/close file,fp=fopen(“file.txt”,“r”);,open file.txt,read in ASCII,Opening a file means,.,returns a file pointer,specify the operation to be done,defining a file name first,points to the first,location of file buffer,.,r-read,file.txt,which The returned value is assigned to a file pointer.,hapter Seven,Open/close filefp=fopen(“fil,11,If process is successful,the returned value is a file pointer.(address),FLIE *fp;,Otherwise,return NULL pointer.,if(fp=fopen(“file.txt”“r”)=NULL),printf(“cant open file n”);,exist(1);,Open/close file,Symbol,Explanation,r,read,w,write,a,append,t,Text stream,b,Binary stream,+,Permit read/write,If fopen fails,return value is zero(NULL pointer)when:,1)fopen wants to open a non existed file for reading.,2)read a file not permitted read.,3)create a file but disk is full.,4)disk sector is bad.,hapter Seven,If process is successful,th,12,3 Close a file,Close a file opened,argument&a pointer to file to be closed in order to 1)release fp and 2)clean up the buffer.,fclose(fp);,Open/close file,Opened file must be closed before ending a program,otherwise,data may be lost.,hapter Seven,3 Close a fileClose a file ope,13,Writes string into file pointed by stream,File read/write,fgets(string,n,stream),Reads a string of up to n-1 characters from stream and places them into string,fputs(string,stream),1 read/write file functions,fgetc(fp),fputc(fp),read/write a char from/to file,read:c=fgetc(fp);read a char from named file.,write:fputc(c,fp);write c to named file.,Prototype in stdio.h,return EOF(-1)if a)at the end of file;,b)detecting an error;,Using function fgets()and fputs()can realize string input/ouput in files.,hapter Seven,Writes string into fi