Input/Output
Note: All the programs mentioned in this lecture are in:/home/cs352/SUMMER02/lecture18progs/
- How does C deal with files? - Identifier FILE is defined in stdio.h. (It is defined as a particular structure whose members describe the current state of the file, no other details necessary.) - Three file pointers, stdin, stdout and stderr are also defined in stdio.h. They're pointers but we refer to them as files. stdin : standard input file - connected to the keyboard. stdout: standard output file - connected to the screen. stderr: standard error file - connected to the screen. - File handling functions fprintf(), fscanf() have prototypes also in stdio.h: int fprintf(FILE *fp, const char *format, ...); /*writes into the file pointed to by fp*/ int fscanf(FILE *fp, const char *format, ...); /*reads from the file pointed to by fp*/ - fprintf(stdout, ...); is the same as printf(...); fscanf(stdin, ...); is the same as scanf(...); (See also the functions fgetc(), fputc(), fgets(), fputs(), which have the prototypes shown in the appendix of the textbook) - sprintf() and sscanf() are the string versions of printf() and scanf(). int sprintf(char *s, const char *format, ...); int sscanf(const char *s, const char *format, ...); sprintf(), writes to its first argument, rather than the screen. sscanf(), reads from its first argument, rather than the keyboard. - Difference between reading from a file (using fscanf()) and reading from a string (using sscanf()): If we use sscanf() to read from string str again then the input starts from the beginning of str, not from where we left off before. see strex.c: - Operations with Files: - Each file can be considered a stream of characters. - It has a name. - It has to be opened and closed (in order to do an operation on it.) - It can be written to, read from or appended to. FILE *fopen(const char *filename, const char *mode); opens, the file named filename in the given mode, and returns a pointer to the file if successful, ow. returns NULL. int fclose(FILE *fp); closes the file pointed to by fp. Returns 0 if closed successfully, ow EOF is returned. - Possible modes: "r" open text file for reading "w" open text file for writing. "a" open text file for appending. Each can end with a + meaning that the file is to be both read and written (updated). - Opening for reading a file that cann ot be read, or doesn't exist will fail. - Opening a file for writing causes the file to be created if it doesn't exist, and causes it to be written o.w. - Opening a file in append mode causes it to be created if it doesn't exist, and causes the writing to occur at the end of the file if it does. - Opening a file in the update mode (using +): Between a read and a write, or a write and a read there must be an intervening call tu fflush() to flush the buffer, or a call to one of the file positioning functions: fseek(), fsetpos(), or rewind() see doublespace.c see uppercase.c - Accessing a file randomly: - Using fseek() and ftell() - ftell(FILE *fp) returns the current value of the file position indicator. (Represents the number of bytes from the beginning of the file, starting from 0, whenever a character is read the counter is incremented by one.) NOTE: The file pointer fp tself does not point to individual characters. fseek(FILE *fp, long offset, int place) sets the file position indicator offset bytes from place. (The value of place can be SEEK_SET- beginning of the file, SEEK_CUR- the current position, or SEEK_END - the end of the file) If successful a zero is returned. - We can also use fread(), and fwrite() for reading from and writing into the file.(Might save time, look at the prototypes in the appendix of the textbook) see backwards.c see random/