Download presentation
Presentation is loading. Please wait.
Published byEmmeline Felicity Leonard Modified over 8 years ago
1
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University
2
Contents 2 Basics of Structures Structures and Functions Arrays/Pointers of Structures Standard I/O File Access Error Handling Low Level I/O
3
Basics of Structures 3 A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Example: struct point { int x; int y };
4
Basics of Structures: Example 4 struct database { int id_number; int age; float salary; }; int main() { struct database employee; employee.age = 22; employee.id_number = 1; employee.salary = 12000.21; printf(“Employee ID = %d, Age = %d and Salary = %7.2f\n”, employee.id_number, employee.age, employee.salary); }
5
Structures and Functions 5 The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members. This could be done by passing components separately, pass an entire structure, or pass a pointer to it.
6
Structures and Functions 6 The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members. This could be done by passing components separately, pass an entire structure, or pass a pointer to it. If a large structure is to be passed to a function, it’s generally more efficient to pass a pointer than to copy the whole structure.
7
Arrays/Pointers of Structures 7. (direct member selector) -> (indirect, or pointer, member selector) struct student_rcd { intstudent_number; charname[128]; }; struct student_rcd { intstudent_number; charname[128]; }; void print_rcd(struct student_rcd rcd) { printf("Number: %d\n",rcd.student_number); printf("Name: %s\n", rcd.name); } void read_rcd(struct student_rcd *rcd) { printf("Enter number: "); scanf("%d", &(rcd->student_number)); printf("Enter name: "); scanf("%s", rcd->name); // (*rcd).name } void print_rcd(struct student_rcd rcd) { printf("Number: %d\n",rcd.student_number); printf("Name: %s\n", rcd.name); } void read_rcd(struct student_rcd *rcd) { printf("Enter number: "); scanf("%d", &(rcd->student_number)); printf("Enter name: "); scanf("%s", rcd->name); // (*rcd).name } int main() { struct student_rcd data_entry; read_rcd(&data_entry); print_rcd(data_entry); return 0;} int main() { struct student_rcd data_entry; read_rcd(&data_entry); print_rcd(data_entry); return 0;}
8
Typedef 8 typedef int Length; // Now Length is a data type. typedef char *String; // Now String is a data type. typedef struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left, *right; /* children */ struct tnode *parent; } Treenode; // Now Treenode is a data type.... Length len, maxlen; Length *lengths[]; String p, lineptr[MAXLINES]; Treenodetnode1;
9
Union 9 A union is a special data type available in C that enables you to store different data types in the same memory location like structures The main difference is that a union may have many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multi-purpose. #include union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; printf( "data.i : %d\n", data.i); return 0; }
10
Standard I/O 10 The simplest input mechanism is to read one character at a time from the standard input with getchar: int getchar(void) getchar returns the next input character each time it is called, or EOF when it encounters end of file. Redirection > & Piping |
11
File Access: Binary Files 11 Binary files are very similar to arrays of structures, except the structures are in a disk-file rather than an array in memory. Binary files have two features that distinguish them from text files: You can instantly use any structure in the file. You can change the contents of a structure anywhere in the file. int fseek(FILE*, long, SEEK_SET or SEEK_CURRENT or SEEK_END); // move file position pointer int fwrite(void*, int memb_size, int no_memb, FILE*); int fread(void*, int memb_size, int no_memb, FILE*); int ftell ( FILE * stream );
12
File Access: Closing 12 The function int fclose(FILE *fp) is the inverse of fopen; It breaks the connection between the file pointer and the external name that was established by fopen, freeing the file pointer for another file. It also flushes the buffer in which putc is collecting output.
13
Error Handling 13 stderr is assigned to a program the same way that stdin and stdout are assigned. Output written on stderr normally appears on the screen. The function ferror returns non-zero if an error occurred on the stream fp int ferror(FILE *fp)
14
Low Level I/O: Read and Write 14 read and write system calls are used by C programs through the two functions: read and write. int n_read = read(int fd, char *buf, int n); int n_written = write(int fd, char *buf, int n); File descriptor Character array where data is to go to or come from Number of bytes to be transferred
15
Low Level I/O: Open, Creat, Close, Unlink 15 Example: Open is rather like the fopen, except that instead of returning a file pointer, it returns a file descriptor, which is just an int. #include int fd; int open(char *name, int flags, int perms); fd = open(name, flags, perms);
16
Questions? 16
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.