Download presentation
Presentation is loading. Please wait.
1
Structures and Unions Chapter 6
2
Structure A structure is an aggregate data type Composed of two or more related variables called member/field/element struct tag-name{ type member1; type member2; type member3;. type memberN; } variable-list;
3
Structure Contd. struct point{ int x; int y; }; Tag-name and variable-list are optional but one of them must present Structure can be initialized strcut point pt = {20, 30}; Member can be accessed via. operator struct point pt; pt.x = 20; pt.y = 30;
4
Array of Structures stuct student{ long id; char name[20]; char dept; }; struct student s[100];
5
Array of Structures Contd. stuct { int id; char name[20]; char dept[4]; } s[100]; scanf(“%d”, &s[0].id); scanf(“%s”, s[0].name); scanf(“%s”, s[0].dept);
6
Nested Structures struct careof{ char houseno[20]; int roadno; char location[80]; char phone[20]; }; struct student_info{ int id; char name[80]; struct careof address; }s;
7
Nested Structures Contd. Accessing the nested structure scanf(“%s”, s.address.houseno); scanf(“%d”, &s.address.roadno);
8
Union union is a single piece of memory that is shared by two or more variables union tag-name{ type member1; type member2; type member3;. type memberN; } variable-list;
9
Union Contd. union u_type{ int i; char c[2]; float d; }sample; 1234 d c[0]c[1] i
10
Suppose that a constant may be an int, a float, or a char struct { char name[20]; int utype; union u_tag{ int ival; float fval; char cval; }u; }symtab[MAX];
11
Structure: Bit-fields So far we cannot access at bit level Bit-fields are useful when you want to pack information into the smallest possible space struct b_type{ unsigned dept: 3; unsigned stock: 2; }var_list;
12
Structure: Bit-fields Contd. The members are all either integer or unsigned integer For integer the left most bit will be regarded as sign bit Can be assigned values confirming its limit A field may overlap a word boundary is implementation-defined Fields may not be named (: and width) used for padding Processor architecture dependent
13
File I/O Chapter 9 Teach Yourself by Herbert Schildt
14
Understanding Streams Stream: the C I/O system supplies a consistent interface to the programmer for device I/O files A level of abstraction A logical interface File: actual device providing I/O is called a file
15
Standard Streams stdin stdout stderr
16
Types of Stream Two types Text Binary Text file Contains ASCII characters Some character translation So, no one-to-one correspondence between what is sent to the stream and what is written to the file Binary file May be used with any type of data No character translation So there is one-to-one correspondence
17
How to open a file? FILE *fopen (char *filename, char *mode); Stdio.h Filename path Mode “r”, “w”, “a”, “rb”, “ab”, “r+” Consequence of those modes
18
How to close a file? Int fclose (FILE *fp); In order to improve efficiency most file system write data to disk one sector at a time Fclose flushes the buffer
19
How to know it is the end of file? Int feof (FILE *fp); Int ferror (FILE *fp);
20
How to read and write a character? Int fgetc (FILE *fp); Int fputc (int ch, FILE *fp); And more on this later
21
An example: reading a text file and displaying it in the screen FILE *fp; if ((fp = fopen (“a.txt”, “r”))==NULL){ //error and exit } while (! feof (fp)){ putchar (fgetc(fp)); } fclose (fp);
22
Writing and reading strings and others Int fputs (char *str, FILE *fp); Int fgets (char *str, int num, FILE *fp); Int fprintf (FILE *fp, format speci, variable(s)); Int fscanf (FILE *fp, format speci, address(es));
23
How to read/write in binary mode? size_t fread (void *buffer, size_t size, size_t num, FILE *fp); size_t fwrite (void *buffer, size_t size, size_t num, FILE *fp); size_t: defined in stdio.h unsigned long Void pointer: pointer of any data types
24
An example: reading a text file and displaying it in the screen FILE *fp; char ch; if ((fp = fopen (“a.txt”, “rb”))==NULL){ //error and exit } while (! feof (fp)){ fread(&ch, sizeof (char), 1); putchar (ch); } fclose (fp);
25
Writing an entire array double d[10] = {10.2, 20.3,….}; fwrite (d, sizeof d, 1, fp); //entire fread (d, sizeof (double), 5, fp); //only first five elements
26
Random Access So far, we have seen write and read sequentially Beginning to end Using another function we can access any point in a file Used only in binary mode (one-to-one) int fseek (FILE *fp, long offset, int origin);
27
Origins are SEEK_SET//seek from the start of file SEEK_CUR//seek from current location SEEK_END//seek from end of file long ftell (FILE *fp);
28
Example: copy a file into another in reverse order FILE *in, *out; char ch; long loc; fseek(in, 0L, SEEK_END); loc = ftell(in); loc = loc -1; //skip the end marker while (loc >= 0){ fseek(in, loc, SEEK_SET); ch = fgetc(in); fputc(ch, out); loc--; }
29
Example?? 10 double numbers are written in a file User want to access any number
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.