Introduction to C Programming ET2560 Introduction to C Programming Introduction to C Programming Unit 9 Manipulating Strings File Processing Unit 1 Presentations
Unit 8 Review Unit 9: Review of Past Material
Unit 8 Review Arrays Collection of adjacent memory cells Each element has the same data type Array declaration must include size in brackets, after name Array size is a positive integer To access array element, follow the name with subscript Subscript is an integer expression (index) in square brackets Array initializer list in braces Parallel arrays are 2 or more related arrays with identical size Can be passed as argument to function, but not returned
Strings String is not a recognized separate data type in C String variable is an array of characters Sized to accept the maximum number of characters Characters start with element zero All strings must end with a zero-value character '\0'
String Library Functions Unit 9: String Manipulation and File Processing
String Library Since strings are arrays, not single variables, can't use operators Instead, the string library provides functions strcpy(src, dest) - copy "src" (source) to "dest" (destination) strlen(s) - returns the length of string "s" (without 0 terminator) strcat(src, dest) - concatenate "src" to end of "dest" strcmp(s1, s2) - compares "s1" to "s2", returns an int code If s1 precedes s2, returns negative value If s2 precedes s1, returns positive value If equal, returns zero sprintf() - puts formatted data in a string sscanf() - gets formatted data from a string
Using Files in C Unit 9: String Manipulation and File Processing
Files in C All file functions require a file pointer (FILE *) The file library functions (in <stdio.h>) perform file ops Must open a file before using it fopen() library function - returns a file pointer Can open for binary or text file access Text files store data in readable characters - easy to change with editor Binary files store data in machine format - faster to read and write Can open for reading or writing Reading - accesses but does not change a file, used for input Writing - empties or creates a file, then program outputs data When finished using a file, close it with fclose()
Working With Text Files Text files treated like sequential stream of bytes Typically processed from beginning to end To output data to a text file fprintf() - Like printf(), but output goes to the file putchar() - Can output one character at a time to the file puts() - Can output a string to the file To input data from a text file fscanf() - Like scanf(), but input comes from the file getchar() - Can input one character at a time from the file gets() - Can input a string from the file
Working With Binary Files File is a sequential collection of binary data bytes Typically processed from beginning to end Function fwrite() used to output binary data Must provide address of variable, number of bytes, a multiplier (useful for an array size), and file pointer Function fread() used to input binary data