Ch8 file processing

1,624 views

Published on

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,624
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
68
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Ch8 file processing

  1. 1. Chapter 8 FILE PROCCESSING 8.1 Introduction 8.2 Introduction to File I/O 8.2.1 declaring FILE variables 8.2.2 opening a Disk Files for I/O file open verification 8.2.3 reading and writing to Disk File 8.2.4 closing a Disk Files 8.2.5 EOF
  2. 2. 8.1 Introduction <ul><li>Why have files : </li></ul><ul><ul><ul><ul><li>Large volume of i/p or o/p data </li></ul></ul></ul></ul><ul><ul><ul><ul><li>More permanent storage of data </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Transfer to other programs </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Multiple simultaneous i/p and o/p </li></ul></ul></ul></ul><ul><ul><ul><ul><li>output streams </li></ul></ul></ul></ul><ul><li>Data Files </li></ul><ul><ul><ul><li>Business Data: </li></ul></ul></ul><ul><ul><ul><ul><ul><li>customer files, payroll files, … </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Scientific Data: </li></ul></ul></ul><ul><ul><ul><ul><ul><li>weather data, environmental data, topographic maps, … </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Image Data: </li></ul></ul></ul><ul><ul><ul><ul><ul><li>web images, satellite images, medical images, … </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Web Data: </li></ul></ul></ul><ul><ul><ul><ul><ul><li>HTML, GIF, JPEG, PNG, XML, … </li></ul></ul></ul></ul></ul>
  3. 3. 8.1 Introduction (cont..) <ul><li>Data Files : a named collection of </li></ul><ul><li> records. normally kept in </li></ul><ul><li> external storage. </li></ul><ul><li>C can process 2 types of files : </li></ul><ul><li> (i) Text files </li></ul><ul><ul><li> (ii) Binary files </li></ul></ul>
  4. 4. 8.1 Introduction (cont..) <ul><li>3 basic operations : </li></ul><ul><li>(i) create file </li></ul><ul><li>(ii) edit file </li></ul><ul><li>(iii) read data from file </li></ul>
  5. 5. 8.2 Introduction to file I/O <ul><li>Steps to work with file </li></ul><ul><li>Begin </li></ul><ul><li>declare variable to be of type file </li></ul><ul><li>fopen( ) function </li></ul><ul><li>perform I/O </li></ul><ul><li>fclose ( ) </li></ul><ul><li>End </li></ul>
  6. 6. 8.2.1 Declaring FILE variable <ul><li>Declaring a file pointer variable : </li></ul><ul><li>FILE *fp ; </li></ul><ul><li>uppercase fp is a pointer to a file </li></ul><ul><li>fp data_file </li></ul>
  7. 7. <ul><li>/*a program that able to read name & age and print the data in data file named biodata.dat*/ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void main() { </li></ul><ul><li>FILE *failnama; </li></ul><ul><li>failnama=fopen(&quot;c:biodata2.dat&quot;,&quot;w&quot;); </li></ul><ul><li>char nama[20]; </li></ul><ul><li>int umur; </li></ul><ul><li>fputs(&quot;Namat umurn&quot;,failnama); </li></ul><ul><li>fputs(&quot;=========================n&quot;,failnama); </li></ul><ul><li>for(int i=0;i<=3;i++) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;Nama:&quot;); </li></ul><ul><li>scanf(&quot;%s&quot;,nama); </li></ul><ul><li>printf(&quot;Umur:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&umur); </li></ul><ul><li>fprintf(failnama,&quot;%st %dn&quot;,nama,umur); </li></ul><ul><li>} </li></ul><ul><li>fclose(failnama); </li></ul><ul><li>} </li></ul>
  8. 8. <ul><li>use fopen( ) to open a specified file. </li></ul><ul><li>fp = fopen(filename, mode) </li></ul><ul><li>e.g : </li></ul><ul><li>FILE *fp; </li></ul><ul><li>fp = fopen(“c:biodata.dat”, “w”); </li></ul><ul><li>fp biodata.xls </li></ul>8.2.2 Opening a File, fopen previous sourcecode
  9. 9. 8.2.2 Opening a File (cont..) <ul><li>FILE *fp; </li></ul><ul><li>fp = fopen(“c:biodata.xls”, “w”); </li></ul><ul><li>Will open the disk file biodata.xls for writing </li></ul>
  10. 10. 8.2.2 Opening a File (cont..) Table: 8.1 Filename and mode Read & write, but append instead of overwrite “ a+” Open file to write, but append instead of overwrite “ a” Read & Write, do not destroy file if it exists “ r+” Open file to read “ r” Read & write, but overwrite file if it exists “ w+” Open file to write (create a file) “ w” description Mode
  11. 11. mode (r) <ul><li>/*a program that read the data from biodata.dat*/ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void main(){ </li></ul><ul><li>char nama[30]; </li></ul><ul><li>int umur; </li></ul><ul><li>FILE *failnama; </li></ul><ul><li>failnama=fopen(&quot;c:biodata.dat&quot;,&quot;r&quot;); </li></ul><ul><li>printf(&quot;Namat umurn&quot;); </li></ul><ul><li>printf(&quot;--------------------n&quot;); </li></ul><ul><li>for(int i=0;i<=3;i++) </li></ul><ul><li>{ </li></ul><ul><li>fscanf(failnama,&quot;%st %dn&quot;,nama,&umur); </li></ul><ul><li>printf(&quot;%st %dn&quot;,nama,umur); </li></ul><ul><li>} </li></ul><ul><li>fclose(failnama); </li></ul><ul><li>} </li></ul>
  12. 12. mode (a) <ul><li>//a program that able to append the data into biodata.dat </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void main(){ </li></ul><ul><li>FILE *failnama1; </li></ul><ul><li>FILE *failnama2; </li></ul><ul><li>failnama1=fopen(&quot;c:biodata.dat&quot;,&quot;a+&quot;); </li></ul><ul><li>failnama2=fopen(“c:student.xls”,”w+”); </li></ul><ul><li>char nama[20]; </li></ul><ul><li>int umur; </li></ul><ul><li>for(int i=0;i<=2;i++) { </li></ul><ul><li>printf(&quot;Nama:&quot;); </li></ul><ul><li>scanf(&quot;%s&quot;,nama); </li></ul><ul><li>printf(&quot;Umur:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&umur); </li></ul><ul><li>fprintf(failnama1,&quot;%st %dn&quot;,nama,umur); </li></ul><ul><li>fprintf(failnama2,&quot;%st %dn&quot;,nama,umur); </li></ul><ul><li>} </li></ul><ul><li>fclose(failnama1); </li></ul><ul><li>fclose(failnama2); </li></ul><ul><li>} </li></ul>
  13. 13. <ul><li>#include <stdio.h> </li></ul><ul><li>void main () { </li></ul><ul><li>FILE *failnama; </li></ul><ul><li>failnama=fopen(&quot;c:buku2.xls&quot;,&quot;w&quot;); </li></ul><ul><li>struct book_info { </li></ul><ul><li>int book_no; </li></ul><ul><li> int year; </li></ul><ul><li>int edition; </li></ul><ul><li>double price; </li></ul><ul><li>} book; </li></ul><ul><li>for(int i=0;i<=1;i++){ </li></ul><ul><li>printf(&quot;nBOOK NUMBER : &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&book.book_no); </li></ul><ul><li>printf(&quot;nYEAR OF PUBLISHED : &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&book.year); </li></ul><ul><li>printf(&quot;nEDITION : &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&book.edition); </li></ul><ul><li>printf(&quot;nPRICE : RM &quot;); </li></ul><ul><li>scanf(&quot;%lf&quot;,&book.price); </li></ul><ul><li>} </li></ul><ul><li>for(i=0;i<=1;i++) { </li></ul><ul><li>printf(&quot;nBOOK NUMBER : %d&quot;, book.book_no); </li></ul><ul><li>printf(&quot;nYEAR OF PUBLISHED : %d&quot;, book.year); </li></ul><ul><li>printf(&quot;nEDITION : %d&quot;, book.edition); </li></ul><ul><li>printf(&quot;nPRICE : RM %.2lfn&quot;, book.price); </li></ul><ul><li>} </li></ul><ul><li>for(i=0;i<=1;i++) { </li></ul><ul><li>fprintf(failnama,&quot;book number: %dt year: %dt edition: %dt price: %.2fn&quot;,book.book_no,book.year,book.edition,book.price); </li></ul><ul><li>} </li></ul><ul><li>fclose(failnama); </li></ul><ul><li>} </li></ul>
  14. 14. <ul><li>fopen returns NULL if the file could not be opened in the mode requested. </li></ul><ul><li>When the file cannot be opened, a suitable error message is displayed. </li></ul>File open verification
  15. 15. File open verification (cont..) <ul><li>#include<stdio.h> </li></ul><ul><li>#define NULL 0 </li></ul><ul><li>void main( ) { </li></ul><ul><li>char nama[30]; </li></ul><ul><li>int umur; </li></ul><ul><li>FILE *failnama; </li></ul><ul><li>failnama = fopen(&quot;c:biodata.dat&quot;,&quot;r+&quot;); </li></ul><ul><li>if (failnama == NULL) </li></ul><ul><li>printf(&quot;n ERROR - Can't open the filen&quot;); </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;Namatumurn&quot;); </li></ul><ul><li>printf(&quot;--------------------n&quot;); </li></ul><ul><li>for(int i=0;i<=3;i++) </li></ul><ul><li>{ </li></ul><ul><li>fscanf(failnama,&quot;%st %dn&quot;,nama,&umur); </li></ul><ul><li>printf(&quot;%st %dn&quot;,nama,umur); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>fclose(failnama); </li></ul><ul><li>} </li></ul>
  16. 16. 8.2.3 Writing to Disk File <ul><li>Prototype: </li></ul><ul><li>fprintf(FILE *fp,output_format,variable); </li></ul><ul><li>e.g </li></ul><ul><li>fprintf(fp,”%s %dn” , name,age); </li></ul>previous sourcecode
  17. 17. fputs( ) <ul><li>#include<stdio.h> </li></ul><ul><li>FILE *fp; </li></ul><ul><li>void main(){ </li></ul><ul><li>fp=fopen(&quot;c:menteri.xls&quot;,&quot;w&quot;);//Bina fail baru </li></ul><ul><li>fputs(&quot;Abdullah Badawin&quot;,fp); </li></ul><ul><li>fputs(&quot;Najib Razakn&quot;,fp); </li></ul><ul><li>fputs(&quot;Muhyiddin Yassinn&quot;,fp); </li></ul><ul><li>fputs(&quot;Azmi Khalidn&quot;,fp); </li></ul><ul><li>fclose(fp);//tutup fail </li></ul><ul><li>} </li></ul>
  18. 18. 8.2.3 Reading to Disk File <ul><li>Prototype: </li></ul><ul><li>fscanf(FILE *fp,input_format,address of variable); </li></ul><ul><li>e.g </li></ul><ul><li>fscanf(fp,”%s %dn” , &name,&age); </li></ul>previous sourcecode
  19. 19. fgets( ) <ul><li>#include <stdio.h> </li></ul><ul><li>#include <string.h> </li></ul><ul><li>void main ( ) { </li></ul><ul><li>FILE *filePtr; </li></ul><ul><li>char person[100]; </li></ul><ul><li>int bil; </li></ul><ul><li>filePtr=fopen(&quot;menteri.dat&quot;,&quot;r&quot;); </li></ul><ul><li>/*baca string dari fail*/ </li></ul><ul><li>for (bil=1;bil<=7;bil++) </li></ul><ul><li>{ </li></ul><ul><li>fgets(person,strlen(person),filePtr); </li></ul><ul><li>puts(person); </li></ul><ul><li>} </li></ul><ul><li>fclose(filePtr); </li></ul><ul><li>} </li></ul>
  20. 20. 8.2.4 Closing a Disk File <ul><li>fclose() function closes the file that was opened by a call to the fopen() function </li></ul><ul><li>Statement: </li></ul><ul><li>fclose(fp); </li></ul>fp is a pointer file variable
  21. 21. End of File (EOF) <ul><li>eof marker is operating system defined & indicate no more data is to be read. </li></ul><ul><li>windows <ctrl> z </li></ul><ul><li>returns 0 if eof not found. </li></ul><ul><li>otherwise, returns 1. </li></ul>
  22. 22. End of File (EOF) (cont..) <ul><li>#include<stdio.h> </li></ul><ul><li>#define NULL 0 </li></ul><ul><li>void main( ) { </li></ul><ul><li>char nama[30]; </li></ul><ul><li>int umur; </li></ul><ul><li>FILE *pelajar; </li></ul><ul><li>pelajar = fopen(&quot;c:biodata2.dat&quot;,&quot;w+&quot;); </li></ul><ul><li>if (pelajar == NULL) </li></ul><ul><li>printf(&quot;n ERROR - Can't open the filen&quot;); </li></ul><ul><li>else{ </li></ul><ul><li>printf(&quot;Enter eof to end inputn&quot;); </li></ul><ul><li>printf(&quot;Namatumurn&quot;); </li></ul><ul><li>printf(&quot;--------------------n&quot;); </li></ul><ul><li>//while not end of file </li></ul><ul><li>while ( ! feof( stdin ) ) { </li></ul><ul><li> fprintf( pelajar, &quot;%s %d n&quot;, nama, umur ); </li></ul><ul><li> printf(&quot;Nama:&quot;); </li></ul><ul><li> scanf(&quot;%s&quot;,nama); </li></ul><ul><li> printf(&quot;Umur:&quot;); </li></ul><ul><li> scanf(&quot;%d&quot;,&umur); </li></ul><ul><li>}//end while </li></ul><ul><li>fclose(pelajar); </li></ul><ul><li>}//end else </li></ul><ul><li>} </li></ul>

×