C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture14_StructsUnionsEnums

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture14_StructsUnionsEnums

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 ee312 Spring 2008 Lecture 14 1 Announcements Lecture 14 • Read Chapter 11 Pointers • Topics for today - Chapter 22 – Random I/O File Access • Topics for today - Chapter 16 – Structures – Unions – Enumerations – The Good, the Bad and the Ugly ee312 Spring 2008 Lecture 14 2 Random Access • Random access means non sequential access to a file – Random access is most often used with binary files. – Also used with text files, but some additional restrictions apply. • Calling fseek() allows repositioning within a file. It’s header is: int fseek (FILE *stream, long int offset, int fromWhere); – The new file position is determined by offset and fromWhere. – offset is a (possibly negative) byte count relative to the position specified by fromWhere. – fromWhere must have one of the following values: SEEK_SET Beginning of file SEEK_CUR Current file position SEEK_END End of file – it returns a non zero value if an error occurred • Examples of calling fseek: fseek(fp, 0, SEEK_SET); /* move to beginning of file */ fseek(fp, 0, SEEK_END); /* move to end of file */ fseek(fp, -10, SEEK_CUR); /* move back 10 bytes */ 2 ee312 Spring 2008 Lecture 14 3 Random Access • ftell() returns the current file position: long int ftell(FILE *stream); – This may be saved and later supplied to a call of fseek (i.e. in order to go back to the old position): long int file_pos; file_pos = ftell(fp); … fseek(fp, file_pos, SEEK_SET); /* return to previous position */ • The call rewind(fp) is equivalent to fseek(fp, 0, SEEK_SET). ee312 Spring 2008 Lecture 14 4 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 3 ee312 Spring 2008 Lecture 14 5 Overview • Structures – Collection of Values – Members – Records – Fields • Union – Different identifiers – Same Memory Space • Enumerations – Values listed by the developer ee312 Spring 2008 Lecture 14 6 Structures • Aggregation of heterogeneous variables (elements) under one name •...
View Full Document

{[ snackBarMessage ]}

Page1 / 19

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture14_StructsUnionsEnums

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

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