Returns eof when the we reaches the end of le example

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: xt book uses the term \ le position indicator". Return Values: Returns the number of arguments read from the stream fp. Returns EOF when the we reaches the end-of- le. Example: FILE *fp int x if ((fp=fopen("data.txt", "r"))==NULL) { printf("Cannot open file.\n") exit(0) } fscanf(fp, "%d", &x) ... CSC 1500 { Lecture 99 7 Writing to Files | fprintf() Function Prototype: #include <stdio.h> int fprintf(FILE *fp, format_specifier, arg1, ...) Function Description: Similar to the use of printf(), except that the data is written to the stream fp (instead of to the standard output). Advances the \ le stream" associated with fp. Return Values: Returns the number of characters written to the stream fp. Returns EOF on error. Example: FILE *fp int x if ((fp=fopen("data.txt", "w"))==NULL) { printf("Cannot open file.\n") exit(0) } fprintf(fp, "%s\n", "Hello!") ... CSC 1500 { Lecture 99 8 Closing Files | fclose() Function Prototype: #include <stdio.h> int fclose(FILE *fp) Function Description: Causes the bu ered data associated with the stream fp to be written out to disk and the corresponding le to be closed. Return Values: Upon successful completion, Otherwise EOF is returned. fclose() returns a value of zero. Example: FILE *fp if ((fp=fopen("data.txt", "r"))==NULL) { printf("Cannot open file.\n") exit(0) } ... /* processing the file data */ if (fclose(fp)==EOF) { printf("Cannot close file.\n") exit(0) } CSC 1...
View Full Document

This note was uploaded on 05/23/2010 for the course COMPUTER S CSC1500 taught by Professor Fung during the Spring '10 term at CUHK.

Ask a homework question - tutors are online