100 likes | 186 Views
Announcements. Office hours have been set If you complete labs at home beforehand, no partner needed. CS24 Lecture 2. File I/O Header files Linking objects using pointers. File I/O. That seems like a useful thing to learn. Every* language has a common way of reading in a file:
E N D
Announcements • Office hours have been set • If you complete labs at home beforehand, no partner needed
CS24 Lecture 2 • File I/O • Header files • Linking objects using pointers
File I/O That seems like a useful thing to learn. • Every* language has a common way of reading in a file: • Read line by line into a string • Parse the string • Every* language has multiple ways of reading from a file • read particular types knowing the format • read character by character • read into an array of words • read a certain number of characters at a time I don’t feel like learning something different for every language *Unverified statement. Every language I have encountered.
File I/O in C • Open file • Read one line in at a time into a string (char []) • Process that string FILE *fopen(const char *path, const char *mode); char *fgets(char *s, int size, FILE *stream); fgets does NOT allocate memory Parsing: char *strtok(char *str, const char *delim); Storing for later: void *malloc(size_t size); size_tstrlen(const char *s);
Print out all lines in file that contain a particular word (grep)
Count & Print out all lines in file that contain a particular word (grep) • Print out the count first • Don’t read in the file twice
header files (.h) • What goes into a header file? • function declarations • global variable declarations “global” • they must to redeclared in *one* .c file • struct declarations • #include necessary libraries • guard to make sure the header file is processed only once • What does not go into a header file? • the code that implements the function • the variable declarations (w/out “global”)
header files (.h) • What goes into a header file? • type definitions • function definitions • global variable declarations • requires “global” keyword, must be declared again in .c • A guard to make sure it is only read once • What does not go into a header file? • Local variable declarations • Function implementations
printFriends.h Guard so printFriends.h is only read once #ifndefPRINTFRIENDS_H #define PRINTFRIENDS_H typedefstruct_friend{ char *lastname, *firstname; char birthdate[9]; } friend; void defaultPrint(friend *, int , FILE *); global intmyVar; #endif Any structs that more than one file will use Any functions dynamicArray.c implements that will be called from outside Any global variables that more than one file will use – must be declared (w/out “global”) in printFriends.c
Linking pointers Typdefstruct _link{ int value; struct _link *next; } link; link l1, l2, l3, l4; link *p1, *p2, *p3, *p4; p1 = &l1; p2 = &l2; p3 = &l3; p4 = &l4; l1.value = 1; l2.value = 2; l3.value = 4; l4.value = 8; p1->next = p2; p2->next = p3; p3->next = p4; p4->next = p1; p1-value = 5; p1->next->value = 3; p1->next->next->value = 7; for(i=0;i<4;i++) { printf(“%d, “,p2->value); p2 = p2->next; } p2 = null; p3 = null; p4 = null;