Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang

Similar presentations


Presentation on theme: "CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang"— Presentation transcript:

1 CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
Input/Output (II) CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang

2 Previously… Conditional Compilation File I/O

3 Agenda File I/O

4 Quiz 6 and the Lecture on April 24
Quiz 6 has 3 questions 30 minutes will be allotted for this quiz, no early turn in allowed The quiz will follow by course evaluation. Material Covered In Quiz 6: Recursion Linked List Lecture 34 – 41 Chapter 13, 14, 16, 22, Section 17.5 in the textbook

5 Redirections Input redirection: Instead of manually typing all the test cases, you can read test cases from a file Hw5 < testcase1 Output redirection: Instead of printing the map to the screen, you can save the result into a file, and use it later HW3_2 > map Why separate stderr and stdout?

6 Text files versus binary files
the files are supposed to be a collection of characters, for instance, the file you typed as source code, or a simple note using notepad Binary: the files that are not supposed to be recognized as characters, for instance, the executables, or the object files (RandMine.o) What about files in MS Words? Why differentiating them? Different format maybe used in storing them

7 File Operations Open a file Close a file Create a file Delete a file
Rename a file

8 Open/Create a file FILE * fopen(const char *filename, const char *mode) FILE * fp; fp = fopen(“c:\\project\\test1.dat”, “r”); if cannot open, check fp for NULL Modes: “r”, “w”, “a”, “r+”, “w+” ,”a+” “rb”, “wb”, “ab”, “rb+”, “wb+”, “ab+”

9 Close a file int fclose(FILE* stream)
close a file no longer used, return 0 if closed successfully, otherwise EOF #include <stdio.h> #include <stdlib.h> main() { FILE *fp; if ((fp = fopen(“hw5.txt”, “r+”)) == NULL) { printf(“Cannot open the file %s\n”, “hw5.txt”); exit(1); } fclose(fp); return 0; Why do we close a file? What happens if we don’t?

10 Delete and Rename a file
int remove(const char * filename) int rename(const char * old, const char * new)

11 Attaching a file to a stream
FILE *freopen(const char *filename, const char *mode, FILE *stream) Open the file and associate it with a particular handle e.g. freopen(“result.txt”, “w”, stdout); e.g. use argv specify the filename from command line, FILE * tmpfile(void) char *tmpnam(char *s) FILE * tempptr; tempptr = tmpfile(); Temp files are closed when 1. the file pointer is closed, or 2. the program ends Disavatage: 1. we don’t know the name of temp file, 2. cannot change mind and decide to make the file permanent tmpnam can be used to generate the name of the temp file

12 File Buffering Not every write goes to the disk directly (again, FedEx store, accumulate packages), the secret? buffering Usually the compiler takes care of this (when buffer is full or file is closed), but sometimes, we can take a more proactive control of the buffers. fflush(fp) push everything in the buffer intended for fp out fflush(NULL) push everything in the buffer for all output out You can also assign a buffer void setbuf (FILE * stream, char * buf) Or have finer control of how buffer works void setvbuf(FILE * stream, char * buf, int mode, size_t, size) Different modes: No buffer One line at a time Accumulate until the buffer is full

13 Types of Input/Output Formatted I/O Character I/O Line based I/O
Block I/O String I/O

14 Formatted I/O printf(const char *format…)
fprintf(FILE * stream, const char *format…) scanf(const char *format…) fscanf(FILE * stream, const char *format…)

15 Character I/O Input Output int putchar(ch)
int fputc(int c, FILE * stream) int putc(int c ,FILE * stream) Output int getchar (void) int getc(FILE * stream) int fgetc(FILE * stream) int ungetc(FILE * stream) ungetc: oops, put it back

16 Line I/O int puts(const char * s)
int fputs(const char * s, FILE * stream) char * gets(char * s) char * fgets(char * s, int n, FILE * stream)

17 Block I/O size_t fread(void * ptr, size_t size, size_t nmemb, FILE * stream) size_t fwrite(const void * ptr, size_t size, size_t nmemb, FILE * stream) char a[100] = {1, 2, 3, …} FILE * fp;//initialize it using fopen, etc size_t number_of_element_written; number_of_element_written = fwrite(a, sizeof(a[0]), sizeof(a)/sizeof(a[0]), fp); What’s the difference between fread(a, 1, 100, fp) and fread(a, 100, 1, fp)? Position yourself

18 Position Yourself int fseek(FILE * stream, long int offset, int whence) SEEK_SET SEEK_CUR SEEK_END fseek(fp, 0L, SEEK_SET) long ftell(FILE *stream)

19 String I/O sprintf(char * s, const char * format, …);
sprintf(str, “%d/%d/%d”, 4, 21, 2006); sscanf(const char * s, const char * format, ….); fgets(str, sizeof(str),stdin); sscanf(str, “%d%d” ,&I, &j)

20 Summary File I/O

21 Before you go Read Chapter 22 Exercise:22.1, 22.9, 22.11
Homework 6 submission deadline today Friday (April 21st) at 11:59 pm Late submission deadline by Sunday at 11:59 pm. Quiz 6 will be on next Monday (April 24th)


Download ppt "CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang"

Similar presentations


Ads by Google