220 likes | 229 Views
Learn about file operations in C programming, including reading and writing strings and numbers, and the differences between text and binary files.
E N D
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 • Reading a string • Writing a string • Reading and writing numbers in file • Text vs. binary file • Reading and writing in binary file
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
Writing string in a file Go to program Enter a few lines of text Hello world Hello world C programming C programming fputs( s, fp ) ; fputs( ) function writes the contents of the array pointer by pointer sto the file pointed by pointer fp
Reading string from a file Go to program 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
New line character • Hello world • programming 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
Cont. Go to program 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
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
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
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
Example program – fprintf Go to program • Input record for employ • Name • Age • Basic salary • Input record as much as user required • Write record in file employee.dat
Example program – fscanf Go to program • Write a program to display the content of file that have been created in the previous example program • Name • Age • Basic salary
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”
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
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
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
Storage of Numbers : No • 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.
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
Reading content from binary file Read from buffer pointer by pointer fp 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 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
Writing content in binary file Memory address to read the content Write the content (read form address specified in 1st parameter) to buffer pointer by pointer fp No of bytes to read from memory address specified in 1st argument Number of Block of size as specified in 2nd parameter to read 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