1 / 15

File pointer

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

bethan
Download Presentation

File pointer

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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”

  3. 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”);

  4. Open mode

  5. 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

  6. 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);

  7. 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)

  8. 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

  9. 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*/

  10. 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

  11. 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

  12. 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.

  13. 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;

  14. 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); • }

  15. fread&fwrite • fread (void *buf, int size, int n, FILE *fp) • fwrite(void *buf, int size, int n, FILE *fp)

More Related