1 / 12

COMP 1402

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

marcelene
Download Presentation

COMP 1402

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. Winter 2009 Tutorial #9 Working with strings and files COMP 1402

  2. Overview of Tutorial #9 • Basic string functions • Safe/unsafe string functions • String versions of scanf/printf/etc • File I/O

  3. 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)‏

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

  5. 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)‏

  6. 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)‏

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

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

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

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

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

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

More Related