1 / 16

File Access, K&R 7.5

File Access, K&R 7.5. Dealing with named files is surprisingly similar to dealing with stdin and stdout. Start by declaring a "file pointer": FILE *fp; /* See Appendix B1.1, pg. 242 */

saskia
Download Presentation

File Access, K&R 7.5

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 Access, K&R 7.5 • Dealing with named files is surprisingly similar to dealing with stdin and stdout. • Start by declaring a "file pointer": FILE *fp; /* See Appendix B1.1, pg. 242 */ • <stdio.h> header contains a structure definition with typedef name FILE that contains component variables (buffer, etc.) used in file I/O • You don't need to know the details of these structs to use simple file I/O • Use standard file access functions such as fopen()

  2. File Access • To open a file • Function prototype fopen : FILE * fopen(const char *name, const char * mode); • To ask fopen to open file (character string "name") in a particular "use mode". fp = fopen(name, mode) • fp is the return value: set to NULL if fopen fails • Legal “use mode” values: "r" for read, "w" for write, and "a" for append. Standard C also allows “r+” for opening a file for read/write; “w+” for creating a file with read/write access; and others(see p.242).

  3. File Access • An "r" mode file can be used like stdin • Function prototype getc: int getc(FILE *stream); • To get a char from an "r" file c = getc(fp); • c is the return value: Return value is the character read from the file OR Set to EOF if getc fails

  4. File Access • A “w“ or “a" mode file can be used like stdout • Function prototype putc: int putc(int c, FILE *stream); • To put a character to a “w” or “a” file status = putc(c, fp) • Return value: set to EOF if putc fails

  5. File Access • When we fopen a file in "w" or "a" mode: • If the file does not already exist, it will be created (like the vi editor creates a new file). • If the file does already exist, then "w" mode fopen will destroy the old contents (like the command mv) and "a" mode will append new contents to the end of the existing file (like "save" command in mail).

  6. File Access • When you have finished reading from a file or writing to a file, call fclose to close the file status = fclose(fp); • Function prototype: int fclose(FILE *stream); • Function fclose( ) returns: Zero for success or EOF if an error occurs

  7. File Access • Every file open requires resources and there is a limit on number of files open at any one time • close each fp when done using it • close all files before program termination • FILE structure has a buffer for disk data in memory • When putc returns data may not get written to file THE DATA IS NOT SAFELY ON DISK YET! • Functions fclose( ) and fflush( ) flush buffer to disk

  8. File Access • When a C program is started, the operating system opens three files and provides file pointers (FILE *) to them: stdin, stdout, and stderr • We can now define getchar and putchar as macros: #define getchar( ) getc(stdin) #define putchar(c) putc((c), stdout) /* why (c) in parens? */ • Other file oriented analogs for input / output functions: int fscanf(FILE *fp, char *format, . . .); /* mode must be "r" int fprintf(FILE *fp, char *format, . . .); /* mode "w" or "a"

  9. Error Handling • Trying to fopen a file that does not exist is an error, • There are other errors as well: reading or writing a file without appropriate permission • There are three streams opened by the O/S when a program begins execution, stdin, stdout, and stderr. • stderr usually goes to the screen even if stdout is redirected to a file prog . . . >outfile /* redirect stdout; overwrite */ prog . . . >&outfile /* redirect stdout and stderr */ prog . . . >>outfile /* redirect stdout; append */

  10. Error Handling • How to write a program so error msgs go to stderr? char *prog = argv[0]; /* pick up command name */ if ((fp = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "%s: can't open %s\n", prog, *argv); exit(1); } • Variable prog is a pointer to char array containing the command name used to invoke this program and *argv is a pointer to char array containing the file name that couldn’t be opened

  11. Error Handling • The exit(int) function (arg: 0-255) terminates program execution and returns argument to invoking process (debugger, shell, fork parent) • Of course, a "return value" from main program would do this as well, but exit() will terminate execution as if we executed a return from main(), and can be called from anywhere in program! • A zero returned by a program means no error • You can use conventions for meaning of non-zero values - best to keep the values positive

  12. Error Handling • To show how the return value may be used: • UNIX conditional sequence && % gcc myprog.c && a.out Second program executes only if first returns = 0 • UNIX conditional sequence || % gcc myprog.c || echo compilation failed Second program executes only if first returns != 0

  13. Error Handling • Note a problem with program on K&R pg. 163: missing a #include • To handle errors, should use: #include <errno.h> • The function ferror() tells us the last error that occurred for a stream if (ferror(stdout)) { fprintf(stderr, “%s: error writing stdout\n”, prog); exit(2); }

  14. Error Handling • If not exiting, to avoid retaining a stale error value from a previous failed operation, use: void clearerr(FILE *stream); • Example: clearerr(stdout);

  15. Error Handling • More generally, errno.h contains a macro "errno" that can be tested; it is zero if there is no problem and non-zero otherwise#define errno (*(_errno())) • Text in B1.7 says errno "may" contain an error number; it will contain one if there has been an error—any error, not just in a stream—unless the error is so serious it corrupted the error structs • We can use the function perror to write out the error msg associated with errno, but we have to test for error right after it occurs to get right one

  16. Error Handling • We can use the function perror to write out standard error message associated with errno, but we have to test for error right after it occurs to get correct one if (errno != 0) { perror("Error at myprog: exiting."); exit(2); } • perror will print out a standard “error message” corresponding to the integer in errno, as if by: fprintf(stderr, "%s: %s\n", s, "error message");

More Related