CSC103: Introduction to Computer and Programming

22
1 CSC103: Introduction to Computer and Programming Lecture No 28

description

CSC103: Introduction to Computer and Programming. Lecture No 28. Previous lecture. Introduction to File File operations Reading from file (character by character) Writing in file (character by character). Today’s lecture outline. File opening mode String (line) I/O in Files - PowerPoint PPT Presentation

Transcript of CSC103: Introduction to Computer and Programming

Page 1: CSC103: Introduction to Computer and Programming

1

CSC103: Introduction to Computer and Programming

Lecture No 28

Page 2: CSC103: Introduction to Computer and Programming

2

Previous lecture

• Introduction to File• File operations–Reading from file (character by character) –Writing in file (character by character)

Page 3: CSC103: Introduction to Computer and Programming

3

Today’s lecture outline

• File opening mode• String (line) I/O in Files–Reading a string–Writing a string

• Reading and writing numbers in file• Text vs. binary file• Reading and writing in binary file

Page 4: CSC103: Introduction to Computer and Programming

4

File opening mode

• "r+" : Reading, writing, modifying content of file, if file does not exist it returns null

• "w+" : Reading, writing, modifying content of file, if file does not exits a new file is created

• "a+" : Reading, adding new contents at the end of file, cannot modify existing contents. if file does not exits a new file is created

Page 5: CSC103: Introduction to Computer and Programming

5

Writing string in a file

fputs ( s, fp ) ; • fputs( ) function writes the contents of the array

pointer by pointer s to the file pointed by pointer fp

Enter a few lines of textHello world Hello worldC programming C programming

Go to program

Page 6: CSC103: Introduction to Computer and Programming

6

Reading string from a file

fgets ( s, 79, fp ) ;• This function read 79 bytes (character) from file

pointed by pointer fp and write in the memory address s.

• s is base address of a character array• This function returns NULL if no record is available to

read

Go to program

Page 7: CSC103: Introduction to Computer and Programming

7

New line character

• If we write the following on file using fputs

• Actual number of character in file are 24• But the size of file will be 26 • Reason : fputs( ) converts the \n to \r\n

combination

Hello worldprogramming

Page 8: CSC103: Introduction to Computer and Programming

8

Cont.

• If we read the same line back using fgets( ) the reverse conversion happens.

• Thus when we write the first line of the poem and a “\n” using two calls to fputs( ), what gets written to the file is

Baa baa black sheep have you any wool \r\n

Go to program

Page 9: CSC103: Introduction to Computer and Programming

9

Writing integer and float in file

• Until now we have seen following functions– fgetc(fp); fputc(ch, fp);– fgets(s, 79, fp); fputs(s, fp)

• C provide functions to write integer and float values in file– fprintf - use to print (write) on file– fscanf - use to scan (read) a file

Page 10: CSC103: Introduction to Computer and Programming

10

fprintf Syntax

• printf(“%s %f %d”, b1.name, b1.price, b1.pages); It prints let us c 350.75 550 on standard output (monitor)

• fprintf(fp, “%s %f %d”, b1.name, b1.price, b1.pages); it prints lets us c 350.75 550 on file pointed by pointer fp

Page 11: CSC103: Introduction to Computer and Programming

11

fscanf syntax

• scanf(“%s %f %d”, b1.name, &b1.price, &b1.pages);It scan/read values (name, price, pages) from standard input (keyboard)

• fscanf(fp, “%s %f %d”, b1.name, &b1.price, &b1.pages );It scan/read values (name price, pages) from a file pointed by pointer fp

Page 12: CSC103: Introduction to Computer and Programming

12

Example program – fprintf

• Input record for employ–Name–Age –Basic salary

• Input record as much as user required• Write record in file employee.dat

Go to program

Page 13: CSC103: Introduction to Computer and Programming

13

Example program – fscanf

• Write a program to display the content of file that have been created in the previous example program–Name–Age –Basic salary

Go to program

Page 14: CSC103: Introduction to Computer and Programming

14

Text Files and Binary Files

• A text file contains only textual information like alphabets, digits and special symbols.

• Actually the ACSII values of these characters are stored in text file

• Example of binary file is .exe file or music data stored in a wave file etc.

• To open binary file we have – “rb” same as “r”– “wb” same as “w”

Page 15: CSC103: Introduction to Computer and Programming

15

Differences between text and binary files

• There are three main areas where text and binary mode files are different. These are: –Handling of newlines –Representation of end of file – Storage of numbers

Page 16: CSC103: Introduction to Computer and Programming

16

Handling of newlines

• In text mode –When writing to file :new line (\n) is

replaced by carriage return plus new line combination (\r\n)–When reading from file: \r\n is replaced by \

n• In binary mode these conversion does not

take place

Page 17: CSC103: Introduction to Computer and Programming

17

End of File

• In text mode, a special character, whose ASCII value is 26, is inserted after the last character in the file to mark the end of file

• In binary mode there is no such special character present to mark the end of file

• The binary mode files keep track of the end of file from the number of characters present in the directory entry of the file

• File written in text mode must be read in text mode• File written in binary mode must be read in binary

mode

Page 18: CSC103: Introduction to Computer and Programming

18

Storage of Numbers • fprintf is the function that is used to write number in

file• How numeric data is stored using fprintf – Character takes one byte– Does float or integer takes 4 bytes– Numbers are stored as string of character e.g 34512

takes 5 bytes and 34566.4563 takes 10 bytes– Thus, numbers with more digits would require more

disk space. • If large amount of numerical data is to be stored in a

file, using text mode may turn out to be inefficient.

: No

Page 19: CSC103: Introduction to Computer and Programming

19

Cont.

• Solution is to used binary mode• Binary mode stores the numbers in binary

format• It means each number would occupy same

number of bytes on disk as it occupies in memory

Page 20: CSC103: Introduction to Computer and Programming

20

Reading content from binary file

• fread() is used to read the content from binary file

fread ( &e, sizeof ( e ), 1, fp )

Read from fp, 1 block containing sizeof(e) bytes and write it at address &e

Memory address to

write content read from fp

No of bytes to read from fp, specified in

4th argument

Number of Block to read, each block size

is specified in 2nd parameter

Read from buffer

pointer by pointer fp

Page 21: CSC103: Introduction to Computer and Programming

21

Writing content in binary file

• fwrite() is used to write the content in binary file

fwrite( &e, sizeof ( e ), 1, fp )

Read 1 block containing sizeof(e) number of bytes from address &e and write it to fp

Memory address to read the content

No of bytes to read from memory

address specified in 1st argument

Number of Block of size as specified in 2nd parameter to

read

Write the content (read form address specified in 1st parameter) to buffer pointer by pointer fp

Page 22: CSC103: Introduction to Computer and Programming

22