160 likes | 510 Views
File pointer. 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 Whether errors or end of file have occurred Syntax
E N D
File pointer • 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 • Whether errors or end of file have occurred • Syntax • FILE *fp; fp is a pointer to a FILE
FILE structure • typedef struct • { int _fd; /*file’s ID*/ • int _cleft; /*characters left in buffer*/ • int _mode; /*mode*/ • char *_nextc; /*current position*/ • char *_buff; /*buffer*/ • } FILE; • #include “stdio.h”
Open a file • fopen • FILE * fopen (char * name, char * mode); • returns a pointer to a FILE. If there is an error, it returns to NULL. • char* name: is a character string containing the name of the file. • char* mode: indicates how one intends to use the file. • example: FILE* fp; fp=fopen(“e:\\a1\\test1.c”, “r”);
Open mode • If a file that does not exist is opened for writing or appending, it is created if possible. So it often be written like this : • FILE *fp; • if ((fp=fopen(“ff.dat”, “r”)==NULL) • { printf (“cannot open this file.\n”); • exit ( 0); • } • exit( ) • Terminates program execution • Argument is available to whatever process called this
Open&Close • fclose • int fclose(FILE *fp) • is called automatically for each open file when a program terminates normally. • it breaks the connection between the file pointer and the external name that was established by fopen, freeing the file pointer for another file. fclose(fp);
getc&putc • getc /*fgetc*/ • int getc(FILE *fp) • returns the next character from the stream referred to by fp • Returns EOF for end of file or error • putc /*fputc*/ • int putc(int c,FILE *fp) • putc writes the character c to the file fp and returns the character written, or EOF if an error occurs. • #define getchar( ) getc(stdin) • #define putchar( ) putc(c,stdout)
getc&putc • stdin : input from keyboard. • stdout: output to screen. • stdin and stdout are objects of type FILE*. They are constants, however not variables, so it is not possible to assign to them • Example Page162
getc&putc-example • #include "stdio.h" • main() { • FILE *fp; char ch; • fp=fopen("e:\\song.txt","w"); • while(ch=getchar()!=‘\n’) • putc(ch,fp); • rewind(fp); /*fclose(fp); fp=fopen("e:\\song.txt","r");*/ • while(!feof(fp)) • printf("%c",getc(fp); • } /*rewind: make fp points to the head of file again*/
Test if a file ended • int feof(FILE *fp) • returns non-zero if end of file ahs occurred on the specified file. • EOF • constant, value is -1
puts&gets • fgets • char*fgets(char* line, int maxn, FILE* fp) • reads the next input line from file fp into character array line; at most maxn-1 characters will be read. The resulting line is terminated with ‘\0’ • Ends when file ends or there is a enter character ’\n’ • Returns line; on end of file or error it returns NULL • fputs • intfputs(char *line, FILE *fp) • writes a string to a file; returns EOF if an error occurs and zero otherwise. • Source code see Page165
fprintf&fscanf • fscanf • int fscanf(FILE *fp, char *format,…) • fprintf • int fprintf(FILE *fp, char *format,…) • formatted input or output of file • are identical to scanf and printf, except that the first argument is a file pointer.
Samples • There is an English letter, it’s file name is letter.txt. The following program is to count the totals of capital letters and sentences. • #include <stdio.h> • main( ) • { • FILE *fp; • char c; • int k, m; • if((fp=fopen(“letter.txt”,”r”))==NULL) • { printf(“cannot open this file.\n”);exit(0);} • k=m=0;
Samples-continued • while(fscanf(fp,”%c”,&c)!=EOF) /*getc(fp)!=EOF*/ • { • if(c<='Z'&&c>='A') k++; • if(c==‘.’) m++; • printf(“%c”, c); • } • printf(“Capital letter numbers:%d\n”, k); • printf(“Sentencd numbers:%d\n”,m); • fclose(fp); • }
fread&fwrite • fread (void *buf, int size, int n, FILE *fp) • fwrite(void *buf, int size, int n, FILE *fp)