lecture19-march6 - Announcements Lecture 19 Assignment 4...

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

View Full Document Right Arrow Icon
Announcements Lecture 19 • Assignment 4 – Designs due today in recitation; Q&A • Topics for today (Read Ch. 16 , skip over 14-15 for now) – Finish file IO – Stacks and The system stack – More on pointers Example /* a program to write 10 double values out to disk file - ask user which one they want to see, read that one back in from the file and display it */ #include <stdio.h> #include <stdlib.h> int main(void) { long int loc; double value; double d[10] = {10.23, 19.87,1002.23,12.9, 0.897,11.45, 75.34, 0.0, 1.01, 875.875}; FILE *fp; if((fp = fopen("myfile", "wb"))==NULL) { printf("Cannot open file.\n"); exit(1);} /* write the entire array in one binary block */ if(fwrite(d, sizeof(d), 1, fp) != 1) { printf("Write error.\n"); exit(1);} fclose(fp); if((fp = fopen("myfile", "rb"))==NULL) { printf("Cannot open file.\n"); exit(1);} printf("Which element would you like to retrieve: (0-9)? "); scanf("%ld", &loc); /* user supplies a number from 0-9 */ if(fseek(fp, loc*sizeof(double), SEEK_SET) != 0) { printf("Seek error.\n"); exit(1);} if(fread(&value, sizeof(double), 1, fp) != 1) { printf(“Read error.\n"); exit(1);} printf("Element %ld is %f", loc, value); fclose(fp); return 0; } I made this example very cryptic so that it would fit on a single slide, bad style Introduction to Stacks • A stack is an ordered set of items stored in a last-in-first-out (LIFO) manner • Adding an item – Referred to as pushing it onto the top of the stack • Removing an item – Referred to as popping it off 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.

Page1 / 3

lecture19-march6 - Announcements Lecture 19 Assignment 4...

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