120 likes | 134 Views
Winter 2009 Tutorial #9 Working with strings and files. COMP 1402. Overview of Tutorial #9. Basic string functions Safe/unsafe string functions String versions of scanf/printf/etc File I/O. String Functions. The course notes are a good reference for the string functions you will need
E N D
Winter 2009 Tutorial #9 Working with strings and files COMP 1402
Overview of Tutorial #9 • Basic string functions • Safe/unsafe string functions • String versions of scanf/printf/etc • File I/O
String Functions • The course notes are a good reference for the string functions you will need • Most functions are obvious, or variations on the printf theme • Man pages are your friend – use them! • The string man page (man string) is a handy reference if you forget function names (but beware non-standard functions)
Basic string functions • strlen – returns the length of a string • strcpy – copies strings. Remember memory allocation is up to you! • strchr, strstr – search for a character or substring in a string • strcmp(a, b) – compare strings. Returns <0 if a<b, 0 if a==b, and >0 if a>b
scanf • You may need to use more advanced scanf patterns for this tutorial • Simple (read a string): scanf(“%s”, str); • Field width (read a string at most 20 chars long): scanf(“%20s”, str) • Pattern matching (read a string containing no X's): scanf(“%[^X]”, str)
scanf/printf on strings • Both scanf and printf can be used on strings (not just I/O) • Prefix function names with an s: sscanf, sprintf, etc • Additional string parameter comes first: • sscanf(input, “%s”, str) • sprintf(output, “%d”, 42)
Safety First • Always check input sizes and use 'counted' versions of string functions where possible: • snprintf, strncmp, strncpy, etc • 'n' versions take an additional parameter to restrict maximum size (see man pages) • The vast majority of real security holes are string buffer overruns! • Never use gets in a real program
File I/O • So far we have only read from the keyboard (standard input or “stdin”) and written to the console (standard output or “stdout”) • Also possible to read/write from/to files in the same way • Actually, in UNIX, stdin and stdout are 'files' • This way, the same program can read user input, files, or another program's output
Opening a file • FILE *fopen(char *filename, char *mode) • Several modes (see man page), you will need “r” (read) and “w” (write) • e.g. to open the file “in.txt” for reading: • FILE *in_file = fopen(“in.txt”, “r”); • Remember to close open files with fclose! • See exercise for working example
Reading/Writing a file • Like strings, the printf/scanf family have file versions • Prefix function names with an 'f' (like 's' for strings) • fprintf, fscanf • First parameter is a FILE* (as returned from fopen)
UNIX: Everything is a file • Remember stdin, stderr and stdout are really just files, you can use them in code • Sometimes the 'f' functions are useful even if you're not using a “real” file: • fprintf(stderr, “ERROR\n”); • fprintf(stdout, “Hello World\n”); • fgets(str, max_str_length, stdin); • Notice fgets is safe, use it even for stdin
Exercises • Download the code tarball from the course webpage • Just one program to write, but uses many string/IO functions • A makefile and stub program are provided