Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.

Similar presentations


Presentation on theme: "CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions."— Presentation transcript:

1 CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions Binary file input / output Case study

2 CP104 Introduction to Programming File I/O Lecture 33 __ 2 Basics about files A file is a sequence (or stream) of bytes. –A text file (or ASCII file) is sequence of ASCII code, i.e., each byte is the 8 bit code of a character. –A binary file contains the original binary number as stored in memory Example: suppose integer 10000 takes four bytes in memory Text file stream (convert each number digit into its ASCII code) Binary file stream (as it is in memory) 00100111 00010000 0010011100010000 001100000010000100110000 10000

3 CP104 Introduction to Programming File I/O Lecture 33 __ 3 File and Devices Files are input/output from memory to devices such as keyboard (stdin), screen (stdout), printer, secondary storage disks Operating systems handle the transmission of data stream between memory and I/O devices. A program uses stdio functions to communicate with operating system to read and write files. The figure is copied from http://www.iu.hio.no/~mark/CTutorial/CTutorial.html

4 CP104 Introduction to Programming File I/O Lecture 33 __ 4 Program, File I/O, and OS FILE *pt; pt = fopen(“file1”, “r”); In compiling, (1) a FILE type structure is allocated in memory for file1, (2) a pointer variable for the FILE type variable is allocated. (3) the function fopen is included, (4) the file name and access mode is passed to the fopen function When the program is running, at fopen function (5) it sends OS a request to open file1, (6) OS then searches the files in the disk If found, (7) it opens a portal and a buffer in memory for the file and passes the address of the buffer and related info of the portal to the FILE variable of file1, and returns the address of the FILE variable to pt. If not found (8) fopen returns NULL pointer to pt. fclose(pt); will make pt NULL, not pointing to the FILE variable for file1. Stdio.h contains FILE type typedef struct{ int _fd; int _cleft; int _mode; char *_nect c char *_buff; } FILE; Other modes for fopen 1.“r”, “rb” 2.“w”, “wb” 3.“a”, “ab” 4.“r+”, “rb+” 5.“w+”, “wb+” 6.“a+”, “ab+” 7.The strings with b are for binary files.

5 CP104 Introduction to Programming File I/O Lecture 33 __ 5 Write and Read Files Function for stdin and stdout 1.printf is a function which write a stream (file) of bytes in memory to the screen in a specified format 2.scanf is a function which read a stream (file) of bytes into memory in a specified format from keyboard. 3.ch = getchar(); 4.putchar(); See code examples Functions for any text file 1.fscanf(pt, “%d”, &num); 2.fprintf(outfilep, “Number = %d\n”, num); 3.ch = getc(infilep); or ch = fgetc(infilep); 4.putc(ch, outfilep); or fputs(ch, outfilep); 5.fread() 6.fwrite()

6 CP104 Introduction to Programming File I/O Lecture 33 __ 6 Program to Make a Backup Copy of a Text File (I)

7 CP104 Introduction to Programming File I/O Lecture 33 __ 7 Program to Make a Backup Copy of a Text File (I)


Download ppt "CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions."

Similar presentations


Ads by Google