1 / 24

Lecture 11

Lecture 11. CIS 208 Friday February 18th , 2005. Wednesday - Review. Email me questions and topics No new material is scheduled. Dynamic 2-d arrays. Must first allocate array of pointers Then allocate each sub-array.

kynton
Download Presentation

Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11 CIS 208 Friday February 18th, 2005

  2. Wednesday - Review • Email me questions and topics • No new material is scheduled.

  3. Dynamic 2-d arrays • Must first allocate array of pointers • Then allocate each sub-array. • When freeing, go in reverse direction, free sub-arrays, then main array.

  4. File I/O • Combines Pointers and console I/O • Most of the same basic functions. • Works on text files or binary files

  5. FILE pointers • point to a stream • FILE *fp; • stdin, stdout are FILE pointers • point to certain place in file.

  6. fopen • Must first open a file FILE *fopen(const char *filename, const char *mode) • mode is how file is to be opened • returns NULL if unsuccessful

  7. mode values r : Open a text file for reading w : Create a text file for writing a : Append to a text file r+ : Open a text file for read/write w+ : Create a text file for read/write etc… Check your book

  8. fopen FILE *fp; fp = fopen(“test.dat”,”w”); • Now we can use the stream pointed to by fp • “w” will create a new file, if one already exists, it overwrites it.

  9. safe fopen • should check to see if open was sucessful. • File might not exist, or protected if ((fp = fopen(“test”,”w”)) == NULL) { printf(“Cannot open file.\n”); exit(1); }

  10. more fopen • Some systems have limit on number of streams • Usually at least 8. Platform dependent • FOPEN_MAX

  11. fclose • Any open file must be closed • int fclose(FILE *fp) readies file for closure • Finishes any leftover writing • Tell OS to formally close file. • unclosed files cause problems • closing == ‘saving’

  12. int fclose(FILE *fp) FILE *fp = fopen(“test”,”w”); … fclose(fp); • fp is now closed and this pointer can be used on something else.

  13. int fclose(FILE *fp) • returns 0 if succesful, • EOF if error • Errors are rare • trying to close an unopened file • closing file on removed storage device.

  14. character writing • Just like console I/O int putc(int ch, FILE *fp); int fputc(int ch, FILE *fp); • ch is a char, even though listed as int. • Only lower byte is sent.

  15. int putc(int ch, FILE *fp) • prints one character to file stream fp • if successful, returns the character • Else, returns EOF

  16. Reading Characters • Just like reading from stdin int getc(FILE *fp) int fgetc(FILE *fp) Both do the same thing. Returns 1 character from stream; Returns EOF if error.

  17. fgetc() char ch; ch = fgetc(fp); do { ch = getc(fp); } while (ch != EOF);

  18. KTOD DTOS • Examples on web page

  19. feof() • in binary files, EOF isn’t a number • int feof(FILE *fp) • given a file pointer, true if it’s reached EOF • false if not • works on text files too.

  20. feof() while (!feof(fp)) { ch = getc(fp); }

  21. FILE and strings • Already used these before int fputs(char *str,FILE *fp) char *fgets(char *str,int length, FILE *fp)

  22. fgets • reads length characters • terminated by newline • writes to pre-allocated character array • must replace newline character with null terminator.

  23. fputs • write entire string to file char *str = “abcd”; fputs(str,fp);

  24. Errors • trying to write to a “r” file • doesn’t destroy file, but isn’t good. • be careful

More Related