#include int main(int argc, char *argv[]) { FILE *fptr; int c; long int indicator; if((fptr = fopen(argv[1], "r")) == NULL) { fprintf( stderr, "no file name argumnet\n" ); exit(1); } while ((c = fgetc( fptr )) != EOF) putchar(c); printf("\nfeof() = %d\n", feof( fptr )); printf("FOPEN_MAX = %d\n", FOPEN_MAX); printf("\nThe first 10 byets of the stream\n"); rewind( fptr ); do { c = fgetc(fptr); putchar(c); indicator = ftell(fptr); } while (indicator < 10 ); printf("\n\nThe last 10 bytes of the stream\n"); fseek( fptr, -10, SEEK_END ); while ((c = fgetc( fptr )) != EOF) putchar(c); fclose( fptr ); exit(0); } /* types size_t FILE fpos_t macros NULL * The possibilities for the third argument to `setvbuf'. * _IOFBF 0 * Fully buffered. * _IOLBF 1 * Line buffered. * _IONBF 2 * No buffering. * * Default buffer size. * BUFSIZ * End of file character. Some things throughout the library rely on this being -1. EOF (-1) * The possibilities for the third argument to `fseek'. These values should not be changed. *g SEEK_SET 0 g* Seek from beginning of file. SEEK_CUR 1 g* Seek from current position. SEEK_END 2 g* Seek from end of file. * Get the values: L_tmpnam How long an array of chars must be to be passed to `tmpnam'. TMP_MAX The minimum number of unique filenames generated by tmpnam (and tempnam when it uses tmpnam's name space), or tempnam (the two are separate). L_ctermid How long an array to pass to `ctermid'. L_cuserid How long an array to pass to `cuserid'. FOPEN_MAX Minimum number of files that can be open at once. FILENAME_MAX Maximum length of a filename. stream text stream binary stream opening file file position indicator fgetc fputc buffering unbuffered fully buffered line buffered setbuf sevbuf closing file functions remove int remove(const char *filename) --- success: 0, not: other rename int rename(const char *old, const char *new) --- success: 0 tmpfile FILE *tempfile(void) tmpfile will be removed automatically at the end of the program tmpnam char *tmpnam(char *s) creates temprary file name. fclose int fclose(FILE *stream) --- success: 0, error: EOF. fflush int fflush(FILE *stream) --- success: 0, error: EOF. forces a write of all user-space bufferd data for the output. fopen FILE *fopen(const char *filename, const char *mode); mode: r w a rb wb ab r+ w+ a+ rb+ wb+ ab+ freopen FILE *freopen(const char *filename, const char *mode, FILE *stream) The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout) setbuf void setbuf(FILE *stream, char *buf) mode = _IOFBF size = BUFSIZ setvbuf int setvbuf(FILE *stream, char *buf, int mode, size_t size); mode: _IOFBF (complete buffering) _IOLBF (line buffering) _IONBF (no buffering) buf: buffer size: array size fprintf int fprintf(FILE *stream, const char *format, ...) fscanf int fscanf(FILE *stream, const char *format, ...) printf int printf(const char *format, ...) equivalent to fprintf( stdout, ... ) scanf int scanf(const char *format, ...) sprintf int sprintf(char *s, const char *format, ...) sscanf int sscanf(const char *s, const char *format, ...) vfprintf intvfprintf(FILE *stream, const char *format, va_list arg) arg: variable argument lists --- requires stdarg.h vprintf int vprintf(const char *format, va_list arg) vsprintf int vsprintf(char *s, const char *format, va_list arg) fgetc int fgetc(FILE *stream) read a character as unsigned char and returns int fgets char *fgets(char *s, int n, FILE *stream) n-1 is the maximal number of characters fputc int fputc(int c, FILE *stream) c is casted to an unsigned char fputs int fputs(constant char *s, FILE *stream) writes a string to stream, without trailing '\0' getc int getc(FILE *stream) getchar int getchar(void) equivalent to getc( stdin ) gets char *gets(char *s) danger, do not use putc int putc(int c, FILE *stream) putchar int putchar(int c) equivalent to putc(c, stdin) puts int puts(const char *s) ungetc int ungetc(intc, FILE *stream) pushes c back to stream, cast to unsigned char only one pushback is guaranteed. fread size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them as the location given by ptr. fwrite size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); fgetpos int fgetpos(FILE *stream, fpos_t *pos) equivalent to ftell, storing the current value of the file offset into or from the object referenced by pos. fseek int fseek(FILE *stream, long int offset, int whence) sets the file position indicator for the stream pointed to by stream. the new position, measured in bytes, is obtained by adding offset bytes to the position specified bye whence. if whence is set to SEEK_SET, SEEK_CUR of SEEK_END, the offset is relative to the start of the file, the current position indicator or the end-of-file, respectively. fsetpos int fsetpos(FILE *stream, const fpos_t *pos) euivalent to fseek ftell long int ftell(FILE *stream) obtains the current value of the file position indicator for the stream pointed to by stream. rewind void rewined(FILE *stream) sets the file position indicator for the stream pointed to by stream to the beginning of the file. it is equivalent to (void)fseek(stream, 0L, SEEK_SET) clearerr void clearerr(FILE *stream) clears the end-of-file and error indicates for the stream pointed to by the stream. feof int feof(FILE *stream) tests the end-of-file indicator for the stream, returning non-zero if it is set. ferror int ferror(FILE *stream) tests the error indicator for the stream perror void perror(const char *s) produces a message on the standard error output popen FILE *popen(const char *command, const char *type) opens a process by creating a pipe, forking, and invoking the shell. pclose int pclose(FILE *stream) */