16
KUKUM Sem2-05/06 EKT120: Computer Programm ing 1 Week 11 – Files

KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

Embed Size (px)

Citation preview

Page 1: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

1

Week 11 – Files

Page 2: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

2

Introduction Almost all of the program developed

before this is interactive In interactive environment, input is via

keyboard and output is via screen/monitor

This type of processing is not suitable if it involves huge amount of input or output to be entered or be displayed on the screen at one time

Therefore, file processing can solve the problem mentioned

Page 3: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

3

File Declaration To implement file processing in C

program, need to #include <stdio.h>

FILE *in_file;FILE *out_file; in_file and out_file are known as

internal file name

Page 4: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

4

Opening File and fopen function Each file must be opened before it is processed While opening the file, external file name need to

be related to the internal file name using fopen function.

Format:internal_filename =fopen(external_filename, mode); Internal file name is the name that the C system

uses to identify a file among others that a program might process

External file name is the name given at “save file as” outside the program e.g. “student.dat”, “student.out” etc

Mode is used to open file

Page 5: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

5

File Mode Basics mode are:

“r” : open file to read “w” : open file to write “a” : append data to the end of an already

existing file “r+” : open and create file to update, i.e. read

and write; did not overwrite previous output “w+” :open and create file for update;

overwrite “a+” : append; open or create file for update

Page 6: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

6

Opening File and fopen function-example

FILE *in_file;FILE *out_file;

in_file = fopen(“student.dat”, “r”);out_file = fopen(“student.out”, “w”);

Page 7: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

7

File Open Verification There is a possibility opening file fails. Maybe the particular file does not exist Therefore need to check or verify

whether the file is successfully opened If file fails to open, need to stop the

program, use exit(-1);if (in_file == NULL){ printf(“File fails to open\n”);

exit(-1);}

Page 8: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

8

File Open Verification-cont

You can also combine open file and file verification

if ((in_file = fopen(“student.dat”, “r”)) == NULL){

printf(“File fails to open\n”);exit(-1);

}

Page 9: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

9

Closing File and fclose function

Each opened file need to be closed Format:

fclose(internal_filename);e.gfclose(in_file);fclose(out_file);

Page 10: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

10

Check for End-of-File and the feof function

Usually you don’t know how many data you want to read from file

Therefore, need to check whether you have reach end of file

Format:feof(internal_filename)

Page 11: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

11

Check for End-of-File and the feof function-exampleFILE *in_file;in_file = fopen(“student.dat”, “r”);if(in_file == NULL){

printf(“Error opening file\n”);exit(-1);

}while(! feof(in_file)){ //stmt to process data }fclose(in_file);

Page 12: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

12

Reading Data from Text Files

Format fscanf(internal file name, format control string, input

list);

fscanf(in_file, “%d”, &marks); fgetc(internal file name);

ch = fgetc(in_file); fgets(string variable, size, internal file

name);fgets(name, 10, in_file);

Page 13: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

13

Writing Data to Text File Format

fprintf(internal file name, format control string, output list);

fprintf(out_file, “%d”, marks); fputc(character expression,internal file name);

fputc(ch, out_file);fputc(“4”, out_file);

fputs(string expression, internal file name);fputs(name, out_file);

fputs(“makan”, out_file);

Page 14: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

14

Sample Program#include <stdio.h>#include <stdlib.h>

int main(void){ FILE *in_file; FILE *out_file; int marks, total=0, count = 0; float avg;

in_file = fopen("student.dat", "r"); out_file= fopen("student.out",

"w"); if(in_file == NULL) { printf("Error opening file\n"); exit(-1); }

while(!feof(in_file)) { fscanf(in_file,"%d",&marks); ++count; total = total + marks; fprintf(out_file, " %d ",marks); } avg = total /count; fprintf(out_file, "\n%.2f\n", avg); fclose(in_file); fclose(out_file);

return 0;}//main

Page 15: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

15

Sample input file and output file

50 60 70 80 9044 55 66 77 8824 56 79 50 77

student.dat

50 60 70 80 90 44 55 66 77 88 24 56 79 50 77 64.00

student.out

Page 16: KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files

KUKUM Sem2-05/06

EKT120: Computer Programming

16

End Week 10 – Files (1)

Q & A!