lecture21-mar10 - Announcements Lecture 21 Friday...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Announcements Lecture 21 • Friday recitations cancelled • Assignment 4 – Due tomorrow – if you haven’t started yet, you probably won’t finish – Q&A • Topics for today (Ch. 16) – Structures – finish – Bit fields – Unions – Enumerations Arrays of Structures #define CLASS_SIZE 142 struct Person_record { char firstName[32]; char lastName[32]; int age; }; typedef Person struct Person_record ; /* Define Person type alias */ . . . Person students [CLASS_SIZE ] ; /* define an array of Persons */ /* fill up the students array from user input */ int i = 0; printf (“input each students information, type in QUIT when done:\n”); do { printf (“input a student\’s first name, last name and age separated \ by spaces:\n”); scanf (“%s %s %d”, students [i].firstName, students [i].lastName, students [i].age); i++; } while (!strcmp(students[i].firstName, “QUIT”) && (i < CLASS_SIZE)) . . . Structs and Files /* models an online inventory of parts in a database(file) */ #include <stdio.h> #include <stdlib.h> #define NAME_LEN 25 #define MAX_PARTS 1000 #define DATA_FILE "inventory.dat" struct part { int part_no; char part_name[NAME_LEN+1]; int num_on_hand; } inventory[MAX_PARTS]; /* the inventory data structure */ int num_parts; /* actual number of parts */ Parts Data File inventory data structure Update Program inventory.dat load save Structs and Files int main(void) { FILE *fp; fp = fopen(DATA_FILE, "rb+" ); if (fp == NULL) { fprintf(stderr, "Can't open %s\n", DATA_FILE); exit(EXIT_FAILURE); } /* read in whole inventory into the array from the data file */ num_parts = fread(inventory, sizeof(struct part), MAX_PARTS, fp); /* modify the inventory as needed in the array */ rewind(fp); /* write the entire array (updated info.) back to the file */ fwrite(inventory, sizeof(struct part), num_parts, fp); fclose(fp); return 0; } for added reliability you should check the value of num_parts after the fread, and the return value of
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/15/2010 for the course EE 16005 taught by Professor Krasner during the Spring '10 term at University of Texas at Austin.

Page1 / 2

lecture21-mar10 - Announcements Lecture 21 Friday...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online