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

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

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 DocumentRight Arrow Icon

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

View Full DocumentRight 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 16 1 Announcements Lecture 16 • Pointers, malloc, linked lists • Review for Exam #2 ee312 Spring 2008 Lecture 16 2 Basic Operations on Files • Open – in a specific mode • Read • Write • Close file1 EOF Disk Name Access mode Size Current location ptr 2 ee312 Spring 2008 Lecture 16 3 File Pointers and Streams • Declarations of functions that perform file I/O appear in <stdio.h>. Each function requires a file pointer as a parameter. • A file pointer is a pointer to a FILE description structure • A program may declare as many file pointers as needed: FILE *fp1, *fp2; • A file pointer represents a stream, which may be a file or—in general—any source of input or output. • Three streams are standard: stdin Standard input stdout Standard output stderr Standard error These streams need not be opened or closed. • Standard input and output can be redirected in both UNIX and Windows: prog <data >result • UNIX also allows redirection of the standard error stream. Only certain versions of Windows (NT and 2000) allow this. Redirection of standard error is done by using 2> instead of >. ee312 Spring 2008 Lecture 16 4 Opening and Closing Files • Files can be opened by calling fopen: FILE *fopen(const char *filename, const char *mode); • mode can be one of the following for text files: "r" Open for reading "w" Open for writing (file need not exist) "a” Open for appending (file need not exist) "r+" Open for reading and writing, starting at beginning "w+" Open for reading and writing (create if not existing) "a+" Open for reading and writing (append if file exists) • mode can be one of the following for binary files “rb”, “wb”, “ab”, “r+b”, “w+b” , “a+b” All with similar meanings as above • If the file cannot be opened, fopen returns a NULL. • Files can be closed by calling fclose: int fclose(FILE *stream); 3 ee312 Spring 2008 Lecture 16 5 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...
View Full Document

This note was uploaded on 04/15/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

Page1 / 27

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

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