1 / 10

Announcements

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:

tahir
Download Presentation

Announcements

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. Announcements • Office hours have been set • If you complete labs at home beforehand, no partner needed

  2. CS24 Lecture 2 • File I/O • Header files • Linking objects using pointers

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

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

  5. Print out all lines in file that contain a particular word (grep)

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

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

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

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

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

More Related