1 / 43

Programming with Standard I/O

Learn about command line arguments, standard I/O, C vs. C++ programming, and create your own "wc" command for counting lines, words, and characters. Includes demonstrations of C string operations, ASCII code, and character testing macros.

Download Presentation

Programming with Standard I/O

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. Programming with Standard I/O CISC3130, Spring 2011 Dr. Zhang

  2. Last Class • Midterm questions review • Command line arguments, standard input/output/error • C Programming vs C++ Programming • No classes or objects • Doesn’t support function overloading • Different standard I/O • Different string operations • Use following commands to copy and extract today’s ex: • cp ~zhang/public_html/CISC3130/Codes/ExCode1.tgz . • tar zxvf ExCode1.tgz

  3. Lab4: your own wc command • Implement your own “wc” command wc [ -l ] [ -w ] [ -c ] [ filename ... ] • Options: • -l: show # of lines • -w: report # of words • -c: report # of characters • If no filename is given, count standard input • List of filenames • Count for each of the file, and then report the total line/word/character numbers for all files

  4. Outline • A script a day • Demonstration of C string: reverse • What’s in a file? • ASCII code, escape sequence • od command • Demonstration of standard I/O in C: vis • printf • getchar, putchar, • Character testing macros

  5. Bash script: watchfor #!/bin/bash PATH=/bin:/usr/bin case $# in 0) echo “Usage: watchfor person” 1>&2; exit 1 esac until who | egrep “$1” do sleep 60 done

  6. Bash script: chanefile #!/bin/bash for i in `ls *.cpp` do #old way: mv $i `basename $i .cpp`.cc mv $i ${i/%.cpp/.cc} done Variable expansion / Substring replacement ${var/%Pattern/Replacement} If suffix of $var matches Pattern, then substitute Replacement for Pattern

  7. Outline • A script a day • Demonstration of C string: reverse • What’s in a file? • ASCII code, escape sequence • od command • Demonstration of standard I/O in C: vis • printf • getchar, putchar, • Character testing macros

  8. A Real C Program: C string • reverse: accepts one command line argument • Treats the argument as a string, and reverses the letters in the string, prints out the reversed string • For example $ ./reverse apple elppa $./reverse spring_song gons_gnirps $./reverse Hello world olleH

  9. Handling strings in C • Command-line arguments are available to main() int main(int argc, char *argv[]) • argc is the number of command line parameters; • parameters are stored in argv[0], ..., argv[argc-1]. (argv[0] is the command name itself.) • Equivalently, int main(int argc, char ** argv) • A “string” is an array of characters, terminated with null character (‘\0’) • char myStr[10]=“Hello”; • Asking for a statically allocated array of characters of size 10, with myStr pointing to first element of the array • Initialize the array with string “hello”

  10. C Library functions for string • #include <string.h> • int strlen (char *s) • Return number of characters in string s, i.e., # of characters untill null character • int strcmp(char * src,char *dest) • Return 0 if src and dest are the same, 1 otherwise • char *strcpy(char *dest, const char *src); • char *strncpy(char *dest, const char *src, size_t n); • Copy src string to dest string… • Look up online manual for details

  11. reverse.c #define MAX 100 int main (int argc, char ** argv) { char buf[MAX], orig[MAX]; int i, len; if (argc<2){ printf (“Invalid usage: must supply a string\n”0; exit(1); } strcpy (orig, argv[1]); len = strlen (orig); // fine the length of the string for (i=0;i<len;i++) buf[i] = orig[len-i-1]; buf[i] = ‘\0’; // null terminate ! printf (buf); }

  12. Compiling on Unix • We can use GNU compiler named gcc gcc –o reverse reverse.cc • run the program $ ./reverse hidave Evadih $ ./reverse This is a long string sihT $./reverse “This is a long string” gnirts gnol a si sihT

  13. Exercise • Implement our own int mystrlen(char * s) function to count the number of characters in string s

  14. Outline • A script a day • Demonstration of C string: reverse • What’s in a text file? • ASCII code, escape sequence • od command • Demonstration of standard I/O in C: vis • printf • getchar, putchar, • Character testing macros 14

  15. Text file vs binary file • Data can be stored in one of two different formats • Text file: info. is stored in ASCII code, can be handled by many Unix commands • Stores numbers as a string of digit characters • Number 12 will be stored as a string “12”, where 1 is a character, 2 is a character • Number 3.1415 stored as a string “3.1415”, … • … • Binary file: info. is stored in binary format, need special program to interpret • Numbers stored as their binary representations • integer 12 stored as 0000000000001100 (16 bits)

  16. What’s inside a file ? • For example, what is inside bash script “watchfor”? #!/bin/bash for i in `ls *.cpp` do #mv $i `basename $i .cpp`.cc mv $i ${i/%.cpp/.cc} done

  17. Octal Dump • “wc” command tells us the file has 93 characters • Octal dump command: od • -c option: interpret bytes as characters [zhang@storm Demo]$ od -c changefile 0000000 # ! / b i n / b a s h \n\n f o r 0000020 i i n ` l s * . c p p ` 0000040 \n d o \n \t # m v $ i ` b a s 0000060 e n a m e $ i . c p p ` . c 0000100 c \n \t m v $ i $ { i / % . c 0000120 p p / . c c } \n d o n e \n 0000135 Positions in the file (base 8)

  18. everything is binary numbers • Remember that everything (numbers, characters) in computer is a stream of 0’s and 1’s • In register, RAM, disk, CD, tape, … • How to represent character in numbers ? • An encoding scheme is needed • ASCII (American Standard Code for Information Interchange) codes represent text in computers, communications equipment, and other devices that work with text.

  19. ASCII code excerpt Decimal Octal Hex Binary Value ------- ----- --- ------ -------------------------------- 000 000 000 00000000 NUL (Null char.) 001 001 001 00000001 SOH (Start of Header) 007 007 007 00000111 BEL (Bell) 008 010 008 00001000 BS (Backspace) 009 011 009 00001001 HT (Horizontal Tab) 010 012 00A 00001010 LF (Line Feed) 011 013 00B 00001011 VT (Vertical Tab) 012 014 00C 00001100 FF (Form Feed) 013 015 00D 00001101 CR (Carriage Return)

  20. ASCII code excerpt Decimal Octal Hex Binary Value ------- ----- --- ------ -------------------------------- 062 076 03E 00111110 > (greater than) 063 077 03F 00111111 ? (question mark) 064 100 040 01000000 @ (AT symbol) 065 101 041 01000001 A 066 102 042 01000010 B 067 103 043 01000011 C 068 104 044 01000100 D 069 105 045 01000101 E 070 106 046 01000110 F

  21. Octal Dump • od –cb : show each byte as octal (base 8) number as well [zhang@storm Demo]$ od -cb changefile 0000000 # ! / b i n / b a s h \n \n f o r 043 041 057 142 151 156 057 142 141 163 150 012 012 146 157 162 0000020 i i n ` l s * . c p p ` 040 151 040 151 156 040 140 154 163 040 052 056 143 160 160 140 0000040 \n d o \n \t # m v $ i ` b a s 012 144 157 012 011 043 155 166 040 044 151 040 140 142 141 163 0000060 e n a m e $ i . c p p ` . c 145 156 141 155 145 040 044 151 040 056 143 160 160 140 056 143 0000100 c \n \t m v $ i $ { i / % . c 143 012 011 155 166 040 044 151 040 044 173 151 057 045 056 143 0000120 p p / . c c } \n d o n e \n 160 160 057 056 143 143 175 012 144 157 156 145 012 0000135

  22. Our observations • Unix uses newline (a special character) to terminate a line • In your editor, you hit “Enter” key to insert a newline into the file • No special character for “end of file” • The file just stops • System simply says there is no more data in the file • e.g. more details later

  23. Escape sequence in C • For special character, we use escape sequence to represent it in C program \' Single quote \" Double quote \\ Backslash \0 Null character (really just the octal number zero) \a Audible bell \b Backspace \f Formfeed \n Newline \r Carriage return \t Horizontal tab \nnn Octal number (nnn), \xnnn Hexadecimal number (nnn) nnn is the ASCII code (in octal) for the character

  24. Outline • A script a day • Demonstration of C string: reverse • What’s in a file? • ASCII code, escape sequence • od command • Demonstration of standard I/O in C: vis • printf • getchar, putchar, • Character testing macros • gets

  25. C: Standard I/O Libraries • Standard I/O library: a collection of routines that provide efficient and portable I/O and system services • Today: read/write to standard input/output • printf: the equivalent of cout in C • scanf: the equivalent of cin in C • getchar, putchar: read/write a single character from standard input/output

  26. Unix online manual • Command man display online manual for commands, library calls, system calls, • man printf: documents command printf • man 3 printf: for C library call printf • Section #’s of online manuals: 1 General commands 2 System calls 3 C library functions 4 Special files (usually devices, those found in /dev) and drivers 5 File formats and conventions 6 Games and screensavers • Miscellanea 8 System administration commands and daemons * Command info: more friendly, detailed info about commands …

  27. A simple C program: vis • Normal file viewing commands, cat, vi, more,… • Do not display non-printing characters • Hard to tell what’s the “space” between columns • vis: • copies its standard input to its standard output • makes all non-printing characters visible by printing them as \nnn, where nnn is the octal value of the character. • useful for checking if there are some weird characters in the file

  28. Simplest I/O routines • char * getchar(): • Gets/returns the next character from standard input • Return EOF when it encounters end of file (or error) • putchar (char c): • Puts the character c on the standard output

  29. Character test macros • Defined in /usr/include/ctype.h • isascii(c): whether c is an ascii code • isalpha(c): alphabetic a-z, A-Z • isdigit(c): digit: 0-9 • ispunct(c): • isprint(c): printable • …

  30. vis program #include <stdio.h> #include <ctype.h> main() { int c; while ((c=getchar()) !=EOF) if (isascii(c) && (isprint(c) || c==‘\n’ || c==‘\t’ || c==‘ ‘)) putchar (c); else printf (“\\%03o”,c); exit(0); }

  31. A program: genfile.c #include <stdio.h> #include <ctype.h> main() { int c; c=0; putchar (c); putchar ('\007'); c='\a'; putchar (c); • putchar ('a'); • putchar ('b'); • putchar ('c'); • putchar ('\n'); • putchar ('\011'); //tab, \t • putchar ('d'); • putchar ('e'); • putchar ('\012'); • } Online resource for ASCII: here

  32. Now try vis [zhang@storm vis]$ ./genfile abc de [zhang@storm vis]$ ./genfile | od -cb 0000000 \0 \a \a a b c \n \t d e \n 000 007 007 141 142 143 012 011 144 145 012 0000013 [zhang@storm vis]$ ./genfile | ./vis \000\007\007abc de

  33. Adding options to vis • vis -s : strip away non-printable character • Command-line arguments are available to main() int main(int argc, char *argv[]) • argc is the number of command line parameters; • parameters are stored in argv[0], ..., argv[argc-1]. Note: argv[0] is the command name itself. • Equivalently, • int main(int argc, char ** argv)

  34. Interpreting complex declaration char *argv[]; • brackets and parentheses (that is, modifiers to the right of the identifier) take precedence over asterisks (that is, modifiers to the left of the identifier). • Read from inside out: • argv is • an array of • pointers to • character … (apply type specifier, i.e., char, last) identifier

  35. Interpreting complex declaration char (*tmp)[]; • brackets and parentheses (that is, modifiers to the right of the identifier) take precedence over asterisks (that is, modifiers to the left of the identifier). • Parenthesis can be used to override default order • Read from inside out, go to right first, then left • tmp is (start with the identifier) • pointer to (interpret what’s in the parenthesis first) • array of (go to right) • character … (apply type specifier, i.e., char, last) identifier

  36. Example of processing options #include <stdio.h> #include <ctype.h> int main(intargc, char *argv[]) { int c, strip = 0; if (argc > 1 && strcmp(argv[1], "-s") == 0) strip = 1; while ((c = getchar()) != EOF) if (isprint(c) || isspace(c) || c==‘\n’ || c==‘\t’ || c==‘ ‘)) ) putchar(c); else if (!strip) printf("\\%03o", c); return 0; } strcmp is part of standard I/O library; it compares two strings and return 0 if two strings are identical.

  37. printf: C’s cout int printf (const char *, … ); • Write strings, integers, doubles, etc to standard output, perform format conversion • e.g., write i=10 as “10”; d=10.23 as “10.230” … • … means “variable number of arguments”, the first argument is required (a string).

  38. Other formatting tags • printf : formatting tag or place holder • %[flags][width][.precision][length]specifier • Specifiers: • d: treat corresponding parameter as a signed integer • u: means unsigned integer • x: means print as hexadecimal • o: means to print the parameter as octal numbers • s: means treat it as a string • c: is for character (char) • f : is for floating point numbers • …

  39. Controlling the output • printf : formatting tag • %[flags][width][.precision][length]specifier • Width: • A number specifying width of output (left padding with space if needed) • Flags: • 0: means left padding the output with 0; default is space • Precision: • the number of digits after decimal points

  40. Fun with printf • printf (“square root of 10 is %20.15f\n”, sqrt(10); square root of 10 is 3.162277660168380 • printf(“\\%03o”,’>’); • print ‘>’ as octal number of 3 character long, left padding with 0 • 076 • char * s = “Hi Dave”; • printf (“the string \”%s\” is %d characters long\n”, s, strlen(s));

  41. Reading a line • You can use function fgets to read an entire line • char *fgets (char *s, int size, FILE * stream); • s is an array of characters • size is the maximum # of chars • FILE is a file handle, for now, remember • stdin (a constant): standard input Read a line (i.e., read characters until newline is met or until reach maximum #) from specified file, and save to the string pointed to by s 41

  42. Example of fgets char s[101]; printf (“Type in your name\n”); fgets (s,100,stdin); printf (“Your name is %s\n”,s);

  43. Next class • File I/O • Pointers and dynamically allocated memory

More Related