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

7
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

Transcript of CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary...

Page 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.

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

Page 2: 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.

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

00100111 00010000

0011000000100001 001100000011000000110000

1 0 0 0 0

Page 3: 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.

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

Page 4: 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.

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 diskIf 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 typetypedef struct{

int _fd;int _cleft;int _mode;char *_nect cchar *_buff;

} FILE;• Other modes for fopen1. “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.

Page 5: 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.

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 file1. 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()

Page 6: 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.

CP104 Introduction to Programming File I/O Lecture 33 __ 6

Program to Make a Backup Copy of a Text File (I)

Page 7: 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.

CP104 Introduction to Programming File I/O Lecture 33 __ 7

Program to Make a Backup Copy of a Text File (I)