1 / 41

Lecture

Lecture. Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass. Standard Library Functions. Standard library functions in K&R Appendix B On line, you can get documentation about them under the corresponding header files:

Télécharger la présentation

Lecture

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. Lecture • Starting K&R Chapter 7 and Appendix B • Also, UNIX – Various chapters in Glass

  2. Standard Library Functions • Standard library functions in K&R Appendix B • On line, you can get documentation about them under the corresponding header files: http://www.digitalmars.com/rtl/stdio.html • On UNIX, use grep to find them in the .h files, e.g. % grep strcmp /usr/include/* • You can get details of them using the man command, e.g. % man printf

  3. UNIX “Pipes”, Glass Pg 175+ • Remember redirection for sysin and sysout? % command <filename1 >filename2 • In general, you can invoke a program and pass sysout from that program as sysin to another program via UNIX pipe – symbol | % command1 arguments | command2 arguments % grep pattern filename(s) | more

  4. Standard Input /Output, 7.1 • C code for reading stdin char getchar (void) gets a character from stdin • C code for writing to stdout void putchar(char) puts a character to stdout • stdin and stdout can be redirected or piped % ./tail <tail.in | more % cat filename1 | ./tail >filename2

  5. Formatted Output, printf, K&R 7.2 • Formats and prints out internal values. int printf(char *format, arg1, arg2, . . .); • printf has a variable length argument list (as many arguments after the first one as % conversions in the format string) • We will learn how to do this shortly • Return from printf is number of characters printed • Haven't used this up till now, but it may be useful if there is some error or limit truncation

  6. Formatted Output, printf • Between the % and the conversion character, there are a number of other characters which may exist. In the order they must be placed, they are: - (minus sign) left adjust printing of argument m (number m) minimum field width . (dot) separates min field width & precision p (integer p) precision: max chars for string min digits for int h or l (letters) h for short int, l for long int • ORDER of options for %d is: %[-][m][.][p][h|l]d, Note: No embedded spaces allowed!

  7. Formatted Output, printf • Figure out what these would do: %10d, %-10d, %hd, %e, %10ld, %10.p • Experiment for 10 minutes with a program, using different formats • Learn string precision given on pg. 154 • Also, to print at most max characters from string s (max is int type var or const), use * after % and include the int max as an argument before s: printf("%.*s", max, s);

  8. Formatted Output, printf • Can print string literal as format string with no “%s” printf("hello, world!\n"); • Could also print a string variable as format string: char s[ ] = "hello, world"; printf(s); • If string s has a % character in it, this is unsafe! • printf will look for another argument after format string s. Better to write out a variable string s as: printf("%s", s);

  9. Formatted Output, sprintf • See sprintf in K&R Appendix B, pg 245 • Function sprintf works same as printf, but it writes to a specified string, e.g. char array[ ], with trailing ‘\0’ int sprintf(char *string, char *format, arg1, arg2, …); • Note: int return value does not include trailing ‘\0’ • Recall how we wrote itoa() and itox() functions • No functions like this in C library! • Use sprintf() to print int into a string using %d or %x

  10. Formatted Input, scanf • This is the opposite of printf. Reads in variables from stdin using conversion format string. See pg. 246 (and prior pg 245 which explains everything). int scanf(char *format, …); • Return value from scanf( ) is number of successfully scanned tokens • Not successful if scanf can't parse any value brought in from stdin according to the specified format

  11. Formatted Input, scanf • Must call scanf with a POINTER to each variable so that values can be set by scanf which is a function! int age, weight, cnt; char lname[100]; while(some condition) { printf("Input your last name, age, and weight"); cnt = scanf("%s %d %d", lname, &age, &weight); } • Note: lname is an array and is already a pointer

  12. Formatted Input, scanf • Scanf is useful to allow you to read in int or double value AS A NUMBER (instead of a character string leaving you to do your own conversion in your code) • scanf() always see a character sequence in stdin: it just does its own conversion to int or double

  13. Formatted Input, scanf • However, scanf is FLAWED, because it ignores '\n' characters. • Can get very confusing if the user enters too few arguments on an input line being parsed by scanf • (Prompt) Input your last name, age, and weight: • (User input) Clinton 52 • User gets no response after carriage return. • User retries, remembers to enter weight this time • (User input) Clinton 52 200

  14. Formatted Input, scanf • scanf sees Clinton 52 Clinton since the user entered carriage return is seen as white space • scanf thinks weight has bad value and returns 2 as number of successfully scanned tokens • scanf has gotten out of synch with input process • Re-entered 52 will be seen as a last name and 200 as an age in next prompt/scanf loop

  15. Formatted Input, scanf • Use scanf only for programs needing only ONE input item, usually "quick and dirty" programs with no input checking. • Can't code defensively with scanf( ): can't count number of tokens parsed ON A LINE - scanf doesn't care about input lines • Best approach is to read a line into an array s[ ] and use "sscanf( )" to parse the arguments in line • This also allows you to try to interpret things in more than one way

  16. Formatted Input, sscanf • See sscanf in K&R Appendix B, pg 246 • Function sscanf works same as scanf, but it reads from a specified string, e.g. char array[ ], with a trailing ‘\0’ int sscanf(char *string, char *format, &arg1, &arg2, …); • Recall how we wrote function atoi, xtoi to convert a decimal or hex character string s to an integer i? • Use sscanf(s, "%d", &i) for atoi • Use sscanf(s, "%x", &i) for xtoi

  17. Formatted Input, scanf/sscanf • Note: with both scanf and sscanf, if you put specific characters in the format string, the functions must see exactly those specific characters in the user input cnt = sscanf(s, "%d/%d/%d", &month, &day, &year); • Expects input to look exactly like this: 07/23/96 • If not, cnt value returned by sscanf is less than 3

  18. Variable Length Argument Lists • Both printf and scanf have an argument (the format string) that defines the number and type of the remaining arguments in the list • C does not support multiple declarations of the same function each with different lists • How is it supported in C? • Look at stack frame after a function call!

  19. Typical Stack frame Stack Pointer After Call Decreasing Addresses Stack Pointer Before Call A r g 1 A r g 2 A r g 3 Function’s Automatic Variables Return Data e.g.PC, other Registers, etc Code provides the location of the last fixed argument in call sequence to va_start Address 0xffffffff From fixed arguments, the code must determine the number of additional arguments to access via offsets from stack pointer and va_arg can work its way back up the stack to get each argument

  20. Variable Length Argument Lists • Use va_list data type and va_ macro package inside function with a variable length argument list to get args • va_start, va_arg, va_end macros are defined in /usr/include/stdarg.h void foo (int n, …) /* note ellipsis … */ { va_list ap; /* variable name ap */ va_start(ap, n); /* n is last named arg */ • Now ap points just before first unnamed arg

  21. Variable Length Argument Lists • Each call to va_arg( ) advances pointer ap by one argument and returns value by type: ival = va_arg(ap, int); fval = va_arg(ap, float); sval = va_arg(ap, char *); • Function must clean up before returning: va_end(ap); }

  22. More examples • Go over the minprintf(char *fmt, …) in K&R p.156

  23. minprintf Program Listing (page 1) /* minprintf program in K&H P.156 */ #include <stdio.h> #include <stdarg.h> void minprintf(char *fmt,...); int main() { int count =5; char * ptr= "life"; minprintf("%s...%d times\n", ptr, count); return 0; }

  24. void minprintf(char *fmt,...) { va_list ap; char *p, *sval; int ival; double dval; va_start(ap, fmt); for (p = fmt; *p; p++){ if (*p != '%'){ putchar(*p); continue; } minprintf Program Listing (page 2)

  25. minprintf Program Listing (page 3) switch(*++p){ case 'd': ival=va_arg(ap, int); printf("%d", ival); break; case 'f': dval=va_arg(ap,double); printf("%f", dval); break; case 's': for (sval = va_arg(ap, char*); *sval; sval++) putchar(*sval); break; default: putchar(*p); break; } } va_end(ap); }

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

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

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

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

  30. File Access • When we fopen a file in "w" or "a" mode: • If the file does not already exist, it will be created (like any 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).

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

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

  33. 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) • 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"

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

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

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

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

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

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

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

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