300 likes | 468 Views
Structures and Unions. Chapter 6. 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 ;. Structure Contd.
E N D
Structures and Unions Chapter 6
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;
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;
Array of Structures stuct student{ long id; char name[20]; char dept; }; struct student s[100];
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);
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;
Nested Structures Contd. • Accessing the nested structure scanf(“%s”, s.address.houseno); scanf(“%d”, &s.address.roadno);
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;
Union Contd. union u_type{ int i; char c[2]; float d; }sample; 1 2 3 4 c[0] c[1] i d
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];
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;
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
File I/O Chapter 9 Teach Yourself by Herbert Schildt
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
Standard Streams • stdin • stdout • stderr
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
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
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
How to know it is the end of file? Int feof (FILE *fp); Int ferror (FILE *fp);
How to read and write a character? Int fgetc (FILE *fp); Int fputc (int ch, FILE *fp); And more on this later
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);
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));
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
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);
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
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);
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);
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--; }
Example?? • 10 double numbers are written in a file • User want to access any number