1 CSC103: Introduction to Computer and Programming Lecture No 13.
CSC103: Introduction to Computer and Programming
description
Transcript of CSC103: Introduction to Computer and Programming
1
CSC103: Introduction to Computer and Programming
Lecture No 28
2
Previous lecture
• Introduction to File• File operations–Reading from file (character by character) –Writing in file (character by character)
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
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
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
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
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
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
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
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
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
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
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
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”
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
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
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
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
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
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
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
22