CSC 1107: Structured Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1107 http://muele.mak.ac.ug block A, level 3, department of computer science institute of open, distance, and elearning Kizito (Makerere University) CSC 1107 November, 2018 1 / 15
Overview 1 Standard Input and Output Formatted Output printf() Formatted Input scanf(), sscanf() Command Line I/O Redirection File Access 2 Assignment Base64 Encoder/Decoder Kizito (Makerere University) CSC 1107 November, 2018 2 / 15
Standard Input and Output Standard Input and Output The library <stdio.h> implements a simple model of input and output int getchar(void); reads one character at a time from the standard input int putchar(int c); puts the character c on the standard output Returns EOF for end of file or error If your source file refers to an input/output library function, it must contain the directive: #include <stdio.h> Kizito (Makerere University) CSC 1107 November, 2018 3 / 15
Standard Input and Output Example lower.c #include <stdio.h> /* getchar(), putchar() */ #include <ctype.h> int main() /* lower: convert input to lower case */ { int c; } while ((c = getchar())!= EOF) putchar(tolower(c)); /* tolower() defined in ctype.h */ return 0; Kizito (Makerere University) CSC 1107 November, 2018 4 / 15
Formatted Output printf() Formatted Output printf() We have already used printf() in previous examples: int printf(char *format, arg1, arg2,...); Converts, formats, and prints its arguments on the standard output under control of the format It returns the number of characters printed Each conversion specification begins with a % and ends with a conversion character Between the % and the conversion character there may be, in order: 1 A minus sign, which specifies left adjustment of the converted argument 2 A number that specifies the minimum field width 3 A period, which separates the field width from the precision 4 A number, the precision, that specifies the maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value, or the minimum number of digits for an integer 5 An h if the integer is to be printed as a short, or l if as a long Kizito (Makerere University) CSC 1107 November, 2018 5 / 15
Formatted Output printf() Characters Basic printf Conversions Characters Argument Type; Printed As d,i int; decimal number o int; unsigned octal number (without a leading zero) x,x int; unsigned hexadecimal number (without a leading 0x or 0X) u int; unsigned decimal number c int; single character s char *; print characters from the string f double; m.dddddd e,e double; m.dddddde ± xx or m.dddddde ± xx g,g double; use %e or %E if the exponent is less than -4 or greater than or equal to the precision; otherwise use %f p void *; pointer % no argument is converted; print a % Kizito (Makerere University) CSC 1107 November, 2018 6 / 15
Formatted Output printf() Examples :%x: :1f: :%.3e: :1.235e+08: :%s: :hello, world: :%10s: :hello, world: :%.10s: :hello, wor: :%-10s: :hello, world: :%s.15: :hello, world: :%s-15: :hello, world : :%15.10s: : hello, wor: :%-15.10s: :hello, wor : Kizito (Makerere University) CSC 1107 November, 2018 7 / 15
Formatted Input scanf(), sscanf() Formatted Input scanf(), sscanf() scanf() reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments sscanf() reads from a string instead of the standard input The arguments must be pointers int scanf(char *format,...) int sscanf(char *string, char *format, arg 1, arg 2,...) The format string may contain: 1 Blanks or tabs, which are not ignored 2 Ordinary characters (not %), which are expected to match the next non-white space character of the input 3 Conversion specifications, consisting of the character %, an optional assignment suppression character *, an optional number specifying a maximum field width, an optional h, l or L indicating the width of the target, and a conversion character Kizito (Makerere University) CSC 1107 November, 2018 8 / 15
Formatted Input scanf(), sscanf() Characters Basic scanf Conversions Characters Argument Type; Input Data d int *; decimal integer i int *; integer. May be in octal (leading 0) or hexadecimal (leading 0x or 0X) o int *; octal integer (with or without leading zero) u unsigned int *; unsigned decimal integer x int *; hexadecimal integer (with or without leading 0x or 0X) c char *; character s char *; character string (not quoted) 1s char *; non-white space character e,f,g float *; floating-point number with optional sign, optional decimal point and optional exponent % no assignment is made; literal % Kizito (Makerere University) CSC 1107 November, 2018 9 / 15
Formatted Input scanf(), sscanf() Formatted Input scanf(), sscanf() Examples 1 Rudimentary calculator #include <stdio.h> main() { double sum = 0, v; while (scanf("%lf", &v) == 1) printf("\t%.2f\n", sum += v); } 2 Suppose input lines should contain dates of a certain form while (getline(line, sizeof(line)) > 0) if (sscanf(line, "%d %s %d", &day, monthname, &year) == 3) printf("valid: %s\n", line); /* 18 October 2018 form */ else if (sscanf(line, "%d/%d/%d", &month, &day, &year) == 3) printf("valid: %s\n", line); /* mm/dd/yy form */ else printf("invalid: %s\n", line); /* invalid form */ Kizito (Makerere University) CSC 1107 November, 2018 10 / 15
Formatted Input scanf(), sscanf() Formatted Input scanf(), sscanf() Examples 1 Rudimentary calculator #include <stdio.h> main() { double sum = 0, v; while (scanf("%lf", &v) == 1) printf("\t%.2f\n", sum += v); } 2 Suppose input lines should contain dates of a certain form while (getline(line, sizeof(line)) > 0) if (sscanf(line, "%d %s %d", &day, monthname, &year) == 3) printf("valid: %s\n", line); /* 18 October 2018 form */ else if (sscanf(line, "%d/%d/%d", &month, &day, &year) == 3) printf("valid: %s\n", line); /* mm/dd/yy form */ else printf("invalid: %s\n", line); /* invalid form */ Kizito (Makerere University) CSC 1107 November, 2018 10 / 15
Command Line I/O Redirection Command Line I/O Redirection Input redirection: prog < input_file Input redirection from another program: prog other_prog Output redirection: prog > output_file Output redirection (append): prog >> output_file Kizito (Makerere University) CSC 1107 November, 2018 11 / 15
File Access File Access FILE *fopen(char *name, char *mode); int getc(file *fp); getchar() getc(stdin) int putc(int c, FILE *fp); putchar(c) putc(c, stdout) int fscanf(file *fp, char *format,...); int fprintf(file *fp, char *format,...); int fclose(file *fp); Kizito (Makerere University) CSC 1107 November, 2018 12 / 15
File Access Line Input, Output, and Error Handling Line I/O char *fgets(char *line, FILE *fp); int fputs(char *line, FILE *fp); Error Handling int ferror(file *fp); int feof(file *fp); Kizito (Makerere University) CSC 1107 November, 2018 13 / 15
File Access File Access Example copyfile.c 1. #include <stdio.h> 2. 3. int main(int argc, char *argv[]) { /* main() can have arguments! */ 4. FILE *fin, *fout; 5. 6. if (argc == 1) { 7. fprintf(stderr, "No file supplied!\n"); 8. return 1; 9. } else if ((fin = fopen(*++argv, "r")) == NULL) { 10. fprintf(stderr, "Can t open file %s for reading\n", argv[1]); 11. return 1; 12. } else if (argc > 2 && (fout = fopen(*++argv, "w")) == NULL) { 13. fprintf(stderr, "Can t open file %s for writing\n", argv[2]); 14. fclose(fin); 15. return 2; 16. } else if (argc == 2) 17. fout = stdout; 18. for (int c; (c = getc(fin))!= EOF; ) /* copy the file */ 19. putc(c, fout); 20. fclose(fin); 21. if (fout!= stdout) fclose(fout); 22. return 0; 23. } Kizito (Makerere University) CSC 1107 November, 2018 14 / 15
Assignment Base64 Encoder/Decoder Assignment IV Input and Output Base64 Encoder/Decoder See uploaded doc. Write an encoder based on the base64 encoding scheme. Kizito (Makerere University) CSC 1107 November, 2018 15 / 15