22
1 Chapter 8 Chapter 8 FILE PROCCESSING FILE PROCCESSING 8.1 Introduction 8.2 Introduction to File I/O 8.2.1declaring FILE variables 8.2.2opening a Disk Files for I/O file open verification 8.2.3reading and writing to Disk File 8.2.4closing a Disk Files 8.2.5 EOF

Ch8 file processing

Embed Size (px)

Citation preview

Page 1: Ch8 file processing

1

Chapter 8Chapter 8FILE PROCCESSINGFILE PROCCESSING

Chapter 8Chapter 8FILE PROCCESSINGFILE PROCCESSING

8.1 Introduction8.2 Introduction to File I/O

8.2.1 declaring FILE variables8.2.2 opening a Disk Files for I/O file open verification8.2.3 reading and writing to Disk File8.2.4 closing a Disk Files8.2.5 EOF

Page 2: Ch8 file processing

2

8.1 Introduction• Why have files :

– Large volume of i/p or o/p data

– More permanent storage of data

– Transfer to other programs

– Multiple simultaneous i/p and o/p

– output streams

• Data Files• Business Data:

» customer files, payroll files, …• Scientific Data:

» weather data, environmental data, topographic maps, …

• Image Data: » web images, satellite images, medical images, …

• Web Data: » HTML, GIF, JPEG, PNG, XML, …

Page 3: Ch8 file processing

3

8.1 Introduction (cont..)

• Data Files : a named collection of records. normally

kept in external storage.

• C can process 2 types of files :

(i) Text files (ii) Binary files

Page 4: Ch8 file processing

4

8.1 Introduction (cont..)

• 3 basic operations :

(i) create file(ii) edit file(iii) read data from file

Page 5: Ch8 file processing

5

8.2 Introduction to file I/O

• Steps to work with fileBegin

(i) declare variable to be of type file(ii) fopen( ) function(iii) perform I/O(iv) fclose ( ) End

Page 6: Ch8 file processing

6

8.2.1 Declaring FILE variable

• Declaring a file pointer variable :

FILE *fp ;

uppercase fp is a pointer to a file

fp data_file

Page 7: Ch8 file processing

UTHM/SEM II 06/07 BEE 1212 7

/*a program that able to read name & age and print the data in data file named biodata.dat*/

#include<stdio.h>void main() {

FILE *failnama;failnama=fopen("c:\\biodata2.dat","w");

char nama[20];int umur;

fputs("Nama\t umur\n",failnama);fputs("=========================\n",failnama);for(int i=0;i<=3;i++){printf("Nama:");scanf("%s",nama);printf("Umur:");scanf("%d",&umur);fprintf(failnama,"%s\t %d\n",nama,umur);}fclose(failnama);

}

Page 8: Ch8 file processing

8

• use fopen( ) to open a specified file. fp = fopen(filename, mode)

e.g :FILE *fp;fp = fopen(“c:\\biodata.dat”,

“w”);

fp biodata.xls

8.2.2 Opening a File, fopen

previous sourcecode

Page 9: Ch8 file processing

9

8.2.2 Opening a File (cont..)

FILE *fp;fp = fopen(“c:\\biodata.xls”, “w”);

• Will open the disk file biodata.xls for writing

Page 10: Ch8 file processing

10

8.2.2 Opening a File (cont..)

Mode description

“w” Open file to write (create a file)

“w+” Read & write, but overwrite file if it exists

“r” Open file to read

“r+” Read & Write, do not destroy file if it exists

“a” Open file to write, but append instead of overwrite

“a+” Read & write, but append instead of overwrite

Table: 8.1 Filename and mode

Page 11: Ch8 file processing

UTHM/SEM II 08/09 DEE2112 11

mode (r)/*a program that read the data from biodata.dat*/#include<stdio.h>void main(){

char nama[30];int umur;

FILE *failnama;failnama=fopen("c:\\biodata.dat","r");

printf("Nama\t umur\n");printf("--------------------\n");for(int i=0;i<=3;i++){

fscanf(failnama,"%s\t %d\n",nama,&umur);printf("%s\t %d\n",nama,umur);

}fclose(failnama);

}

Page 12: Ch8 file processing

12

mode (a)//a program that able to append the data into biodata.dat#include<stdio.h>void main(){

FILE *failnama1;FILE *failnama2;failnama1=fopen("c:\\biodata.dat","a+");failnama2=fopen(“c:\\student.xls”,”w+”);

char nama[20];int umur;for(int i=0;i<=2;i++) {

printf("Nama:");scanf("%s",nama);printf("Umur:");scanf("%d",&umur);fprintf(failnama1,"%s\t %d\n",nama,umur);fprintf(failnama2,"%s\t %d\n",nama,umur);

}fclose(failnama1);fclose(failnama2);

}

Page 13: Ch8 file processing

13

#include <stdio.h>void main () {

FILE *failnama;failnama=fopen("c:\\buku2.xls","w");struct book_info {

int book_no; int year;

int edition; double price;

} book;for(int i=0;i<=1;i++){

printf("\nBOOK NUMBER : ");scanf("%d",&book.book_no);printf("\nYEAR OF PUBLISHED : ");scanf("%d",&book.year);printf("\nEDITION : ");scanf("%d",&book.edition);printf("\nPRICE : RM ");scanf("%lf",&book.price);

}for(i=0;i<=1;i++) {

printf("\nBOOK NUMBER : %d", book.book_no);printf("\nYEAR OF PUBLISHED : %d", book.year);printf("\nEDITION : %d", book.edition);printf("\nPRICE : RM %.2lf\n", book.price);

}for(i=0;i<=1;i++) {

fprintf(failnama,"book number: %d\t year: %d\t edition: %d\t price: %.2f\n",book.book_no,book.year,book.edition,book.price);}fclose(failnama);

}

Page 14: Ch8 file processing

14

• fopen returns NULL if the file could not be opened in the mode requested.

• When the file cannot be opened, a suitable error message is displayed.

File open verification

Page 15: Ch8 file processing

15

File open verification (cont..)#include<stdio.h>#define NULL 0void main( ) {

char nama[30];int umur;FILE *failnama;

failnama = fopen("c:\\biodata.dat","r+");if (failnama == NULL)

printf("\n ERROR - Can't open the file\n");else{

printf("Nama\tumur\n");printf("--------------------\n");for(int i=0;i<=3;i++){

fscanf(failnama,"%s\t %d\n",nama,&umur);printf("%s\t %d\n",nama,umur);

}}

fclose(failnama);}

Page 16: Ch8 file processing

16

8.2.3 Writing to Disk File

• Prototype:fprintf(FILE *fp,output_format,variable);

• e.gfprintf(fp,”%s %d\n” , name,age);

previous sourcecode

Page 17: Ch8 file processing

17

fputs( )#include<stdio.h>

FILE *fp;void main(){

fp=fopen("c:\\menteri.xls","w");//Bina fail barufputs("Abdullah Badawi\n",fp);fputs("Najib Razak\n",fp);fputs("Muhyiddin Yassin\n",fp);fputs("Azmi Khalid\n",fp);

fclose(fp);//tutup fail}

Page 18: Ch8 file processing

18

8.2.3 Reading to Disk File

• Prototype:fscanf(FILE *fp,input_format,address of variable);

• e.gfscanf(fp,”%s %d\n” , &name,&age);

previous sourcecode

Page 19: Ch8 file processing

19

fgets( )#include <stdio.h>#include <string.h>void main ( ) {

FILE *filePtr;

char person[100];int bil;

filePtr=fopen("menteri.dat","r");

/*baca string dari fail*/

for (bil=1;bil<=7;bil++){

fgets(person,strlen(person),filePtr);puts(person);

}fclose(filePtr);

}

Page 20: Ch8 file processing

20

8.2.4 Closing a Disk File

• fclose() function closes the file that was opened by a call to the fopen() function

• Statement:fclose(fp);

fp is a pointer file variable

Page 21: Ch8 file processing

21

End of File (EOF)

• eof marker is operating system defined & indicate no more data is to be read.

windows <ctrl> z

• returns 0 if eof not found.• otherwise, returns 1.

Page 22: Ch8 file processing

22

End of File (EOF) (cont..)#include<stdio.h>#define NULL 0void main( ) {

char nama[30];int umur;FILE *pelajar;pelajar = fopen("c:\\biodata2.dat","w+");

if (pelajar == NULL)printf("\n ERROR - Can't open the file\n");

else{printf("Enter eof to end input\n");printf("Nama\tumur\n");printf("--------------------\n");

//while not end of filewhile ( ! feof( stdin ) ) {

fprintf( pelajar, "%s %d \n", nama, umur ); printf("Nama:"); scanf("%s",nama); printf("Umur:"); scanf("%d",&umur);

}//end whilefclose(pelajar);}//end else

}