3 Unit 8 Review Arrays Collection of adjacent memory cells Each element has the same data typeArray declaration must include size in brackets, after nameArray size is a positive integerTo access array element, follow the name with subscriptSubscript is an integer expression (index) in square bracketsArray initializer list in bracesParallel arrays are 2 or more related arrays with identical sizeCan be passed as argument to function, but not returned
4 Strings String is not a recognized separate data type in C String variable is an array of charactersSized to accept the maximum number of charactersCharacters start with element zeroAll strings must end with a zero-value character '\0'
5 String Library Functions Unit 9: String Manipulation and File Processing
6 String LibrarySince strings are arrays, not single variables, can't use operatorsInstead, the string library provides functionsstrcpy(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 codeIf s1 precedes s2, returns negative valueIf s2 precedes s1, returns positive valueIf equal, returns zerosprintf() - puts formatted data in a stringsscanf() - gets formatted data from a string
7 Using Files in CUnit 9: String Manipulation and File Processing
8 Files in C All file functions require a file pointer (FILE *) The file library functions (in <stdio.h>) perform file opsMust open a file before using itfopen() library function - returns a file pointerCan open for binary or text file accessText files store data in readable characters - easy to change with editorBinary files store data in machine format - faster to read and writeCan open for reading or writingReading - accesses but does not change a file, used for inputWriting - empties or creates a file, then program outputs dataWhen finished using a file, close it with fclose()
9 Working With Text Files Text files treated like sequential stream of bytesTypically processed from beginning to endTo output data to a text filefprintf() - Like printf(), but output goes to the fileputchar() - Can output one character at a time to the fileputs() - Can output a string to the fileTo input data from a text filefscanf() - Like scanf(), but input comes from the filegetchar() - Can input one character at a time from the filegets() - Can input a string from the file
10 Working With Binary Files File is a sequential collection of binary data bytesTypically processed from beginning to endFunction fwrite() used to output binary dataMust provide address of variable, number of bytes, a multiplier (useful for an array size), and file pointerFunction fread() used to input binary data
Your consent to our cookies if you continue to use this website.