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 icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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);
Image of page 2
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 ee312 Spring 2008 Lecture 16 6 Structures Aggregation of heterogeneous variables (elements) under one name The declaration forms a template that can be used to create objects of that structure type struct Person /* Define structure type */ { char firstName[32]; char lastName[32]; int age; };
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern