Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Manipulation Knowledge Understand the concept of file input and output Skills Able to write an application program involving input output files.

Similar presentations


Presentation on theme: "File Manipulation Knowledge Understand the concept of file input and output Skills Able to write an application program involving input output files."— Presentation transcript:

1 File Manipulation Knowledge Understand the concept of file input and output Skills Able to write an application program involving input output files

2 Introduction A file is a collection of data.
A file is a collection of related data stored in an auxiliary storage device.

3 Input Output File Disk can be used to store data permanently.
Most text editor are used to create data files. Data in files can be accessed by an application program. When the data are read into a program, it should be interpreted by the program for analysis and presentation to the user.

4 Input Output File An opened file is either in a “read state”, “write state” or an “error state”. I/O file provided the same operation as you experienced before. Read state in I/O file has same capability as scanf(). Write state in I/O file has same capability as printf().

5 Input Output File Size of file is determined by the number of character. <eof> (end-of-file character) is used to mark the end of file.

6 Input Output File Example:
Fail ini adalah fail teks!<newline>Ia terdiri daripada dua baris.<newline><eof>

7 Types of Accessing File
Two ways of accessing file Sequential Data in files can be accessed in sequential techniques. Analogy : playing music from cassette. Random Data in files can be accessed in random techniques Analogy : playing music from compact disk

8 File open and close Process of file open begins with open a file. In C, fopen()can be used to a open file. When the file is not in used, it should be close by C. fclose() is a useful function to close a file.

9 fopen() fopen() has two parameter: File name to be open.
Type of operation required for file manipulation. Such as, "r" File opened for read only "w" File opened for write only (start at the beginning of file) "a" File open for write only(start at the end of file)

10 fopen() Example: fopen("input.dat", "r") fopen("atur.c", "w")
fopen("output", "a")

11 fopen() Example: fopen("input.dat", "r") fopen("atur.c", "w")
fopen("output", "a") Open file named input.dat for read only

12 fopen() Contoh: fopen("input.dat", "r") fopen("atur.c", "w")
fopen("output", "a") Open file named bernama atur.c For write only (beginning of file)

13 fopen() Contoh: fopen("input.dat", "r") fopen("atur.c", "w")
fopen("output", "a") Open file named output Write only (start at the end file)

14 fopen() fopen() pointed to the successfully opened file. This pointer are needed when data was accessed or write into a file. As usual, this pointer must be assigned to a specific variable.

15 fopen() Example: FILE *fd; int data; fd = fopen("input.dat", "r");
??? Example: FILE *fd; int data; fd = fopen("input.dat", "r"); fscanf(fd, "%d", &data); data ??? input.dat

16 fopen() Example : FILE *fd; int data; fd = fopen("input.dat", "r");
??? Example : FILE *fd; int data; fd = fopen("input.dat", "r"); fscanf(fd, "%d", &data); data ??? 33 input.dat

17 fopen() Example : FILE *fd; int data; fd = fopen("input.dat", "r");
fscanf(fd, "%d", &data); data ??? 33<eof> input.dat

18 fopen() Example : FILE *fd; int data; fd = fopen("input.dat", "r");
fscanf(fd, "%d", &data); data 34 33<eof> input.dat

19 fopen() We can also used the return value to check whether a file was successfully opened or not.

20 fopen() Example (assume: file was existed): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? 33 input.dat

21 fopen() Example (assume: file was existed): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? 33<eof> input.dat

22 fopen() Example (assume: file was existed): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? 33<eof> input.dat

23 fopen() Example (assume: file was existed): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data 34 33<eof> input.dat

24 fopen() In the case of unsuccessful file opened, it will return null pointer. Example: If fopen() try to open file for read ("r") but that file is not exist, null pointer will initiated.

25 fopen() Example (assume: file was not exist): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? _

26 fopen() Example (assume: file was not exist): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? _

27 fopen() Example (assume: file was not exist): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? _

28 fopen() Example (assume: file was not exist): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? Fail tidak boleh dibuka

29 fopen() Example (assume: file was not exist): fd FILE *fd; int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); data ??? exit() was called to end the Program Remember to put #include <stdlib.h> At the beginning of program Fail tidak boleh dibuka

30 fclose() File that was already opened should be close.
fclose() is needed to close the opened file. Only one parameter need for fclose(). It was a pointer used to the opened file.

31 fopen() Example (assume: file was existed): FILE *fd; fd int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); fclose(fd); data 34 33<eof> input.dat

32 fopen() Example (assume: file was existed): FILE *fd; fd int data;
fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &data); fclose(fd); data 34 33<eof> input.dat

33 fprintf() fprintf() can be used to send a data into files.
fprint() is base on printf() function. But, remember to put one parameter (it is file pointer).

34 fprintf() Example: FILE *fd; kuasa ??? fd ??? int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i ??? 33 fail kuasa2

35 fprintf() Example : FILE *fd; kuasa ??? fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i ??? <eof> fail kuasa2

36 fprintf() Example : FILE *fd; kuasa ??? fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i ??? <eof> fail kuasa2

37 fprintf() Example : FILE *fd; kuasa ??? fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i ??? <eof> fail kuasa2

38 fprintf() Example : FILE *fd; kuasa ??? fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i ??? <eof> fail kuasa2

39 fprintf() Example : FILE *fd; kuasa ??? fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 1 <eof> fail kuasa2

40 fprintf() Example : FILE *fd; kuasa ??? fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 1 <eof> fail kuasa2

41 fprintf() Example : FILE *fd; kuasa 1 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 1 <eof> fail kuasa2

42 fprintf() Example : FILE *fd; kuasa 1 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 1 1 ^ 2: 1<eof> fail kuasa2

43 fprintf() Example : FILE *fd; kuasa 1 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 2 1 ^ 2: 1<eof> fail kuasa2

44 fprintf() Example : FILE *fd; kuasa 1 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 2 1 ^ 2: 1<eof> fail kuasa2

45 fprintf() Example : FILE *fd; kuasa 4 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 2 1 ^ 2: 1<eof> fail kuasa2

46 fprintf() Example : FILE *fd; kuasa 4 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 2 Until … 1 ^ 2: 1 2 ^ 2: 4<eof> fail kuasa2

47 fprintf() Example : FILE *fd; kuasa 16 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 4 1 ^ 2: 1 2 ^ 2: 4 3 ^ 2: 9 4 ^ 2: 16<eof> fail kuasa2

48 fprintf() Example : FILE *fd; kuasa 16 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 5 1 ^ 2: 1 2 ^ 2: 4 3 ^ 2: 9 4 ^ 2: 16<eof> fail kuasa2

49 fprintf() Example : FILE *fd; kuasa 16 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 5 1 ^ 2: 1 2 ^ 2: 4 3 ^ 2: 9 4 ^ 2: 16<eof> fail kuasa2

50 fprintf() Example : FILE *fd; kuasa 16 fd int i, kuasa;
fd = fopen("kuasa2", "w"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } for (i=1; i < 5; i++) { kuasa = i * i; fprintf(fd, "%d ^ 2: %d\n", i, kuasa); fclose(fd); i 5 1 ^ 2: 1 2 ^ 2: 4 3 ^ 2: 9 4 ^ 2: 16<eof> fail kuasa2

51 fscanf() fscanf() can be used to accessing data from a files.
fscanf() is based on scanf() function. But, remember to put one parameter (it is file pointer).

52 fscanf() bil ??? gred ??? Example : FILE *fd; matrik ??? i ??? fd ???
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i ??? fd ??? 33 fail input.dat

53 fscanf() bil ??? gred ??? Example : FILE *fd; matrik ??? i ??? fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i ??? fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

54 fscanf() bil ??? gred ??? Example : FILE *fd; matrik ??? i ??? fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i ??? fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

55 fscanf() bil ??? gred ??? Example : FILE *fd; matrik ??? i ??? fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i ??? fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

56 fscanf() bil 4 gred ??? Example : FILE *fd; matrik ??? i ??? fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i ??? fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

57 fscanf() bil 4 gred ??? Example : FILE *fd; matrik ??? i ??? fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i ??? fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

58 fscanf() bil 4 gred ??? Example : FILE *fd; matrik ??? i fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

59 fscanf() bil 4 gred ??? Example : FILE *fd; matrik ??? i fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik ??? i fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

60 fscanf() bil 4 gred 'C' Example : FILE *fd; matrik 1111 i fd fail
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1111 i fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

61 fscanf() bil 4 gred 'C' Example : FILE *fd; matrik 1111 i 1
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1111 i 1 Matrik: Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

62 fscanf() bil 4 gred 'C' Example : FILE *fd; matrik 1111 i 1
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1111 i 1 Matrik: Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

63 fscanf() bil 4 gred 'C' Example : FILE *fd; matrik 1111 i 1
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1111 i 1 Matrik: Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

64 fscanf() bil 4 gred 'B' Example : FILE *fd; matrik 1120 i 1
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1120 i 1 Matrik: Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

65 fscanf() bil 4 gred 'B' Example : FILE *fd; matrik 1120 i 1
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1120 i 1 Matrik: Gred: C Matrik: Gred: B _ fd Until ... 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

66 fscanf() bil 4 gred 'A' Example : FILE *fd; matrik 1124 i 3
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1124 i 3 Matrik: Gred: C Matrik: Gred: B Matrik: Gred: B Matrik: Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

67 fscanf() bil 4 gred 'A' Example : FILE *fd; matrik 1124 i 4
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1124 i 4 Matrik: Gred: C Matrik: Gred: B Matrik: Gred: B Matrik: Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

68 fscanf() bil 4 gred 'A' Example : FILE *fd; matrik 1124 i 4
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1124 i 4 Matrik: Gred: C Matrik: Gred: B Matrik: Gred: B Matrik: Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

69 fscanf() bil 4 gred 'A' Example : FILE *fd; matrik 1124 i 4
int bil, matrik, i; char gred; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } fscanf(fd, "%d", &bil); for (i=0; i < bil; i++) { fscanf(fd, "%d %c", &matrik, &gred); printf("Matrik: %d\tGred: %c\n", matrik, gred); fclose(fd); matrik 1124 i 4 Matrik: Gred: C Matrik: Gred: B Matrik: Gred: B Matrik: Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat

70 Read data from file using fscanf()
fscanf() returns EOF value when it comes to the end of file. This value can also be used to verify the end of file.

71 fscanf() Example: m ??? FILE *fd; int m; char g;
33 fscanf() Example: FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g ??? fail input.dat fd ???

72 fscanf() Example : m ??? FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m ??? fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g ??? fail input.dat fd

73 fscanf() Example : m ??? FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m ??? fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g ??? fail input.dat fd

74 fscanf() Example : m ??? FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m ??? fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g ??? fail input.dat fd

75 fscanf() Example : m ??? FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m ??? fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g ??? fail input.dat fd

76 fscanf() Example : m 1111 FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m 1111 fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g 'C' fail input.dat fd

77 fscanf() Example : m 1111 FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m 1111 fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g 'C' Matrik: Gred: C _ fail input.dat fd

78 fscanf() Example : m 1120 FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m 1120 fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g 'B' Matrik: Gred: C _ fail input.dat fd

79 fscanf() Example : m 1120 FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m 1120 fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g 'B' Matrik: Gred: C Matrik: Gred: B _ fail input.dat fd Until ...

80 fscanf() Example : m 1124 FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m 1124 fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g 'A' Matrik: Gred: C Matrik: Gred: B Matrik: Gred: B Matrik: Gred: A _ fail input.dat fd

81 fscanf() Example : m 1124 FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m 1124 fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g 'A' Matrik: Gred: C Matrik: Gred: B Matrik: Gred: B Matrik: Gred: A _ fail input.dat fd EOF

82 fscanf() Example : m 1124 FILE *fd; int m; char g;
1120 B 1123 B 1124 A<eof> m 1124 fscanf() Example : FILE *fd; int m; char g; fd = fopen("input.dat", "r"); if (fd == NULL) { printf("Fail tidak boleh dibuka"); exit(1); } while (fscanf(fd, "%d %c", &m,&g)!= EOF) printf("Matrik: %d\tGred: %c\n", m, g); fclose(fd); g 'A' Matrik: Gred: C Matrik: Gred: B Matrik: Gred: B Matrik: Gred: A _ fail input.dat fd

83 Another useful function for Input Output file
Read/write function for Single character - getc() - putc() Read/write function for string fgets() fputs()

84 getc() getc() can be use to read one character from a file. It returns EOF value when it comes to the end of file Example: FILE *fd; char aks; ... aks = getc(fd);

85 Example: aks ??? FILE *fd; char aks; fd ???
fd = fopen("teks.dat", "r"); if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd ??? FTSM 43600 UKM Bangi<eof> fail teks.dat

86 Example : aks ??? FILE *fd; char aks; fd fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

87 Example : aks ??? FILE *fd; char aks; fd fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

88 Example : aks ??? FILE *fd; char aks; fd fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

89 Example : aks 'F' FILE *fd; char aks; fd fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

90 Example : aks 'F' FILE *fd; char aks; fd fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

91 Example : aks 'F' FILE *fd; char aks; fd fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

92 Contoh: F_ aks 'F' FILE *fd; char aks; fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

93 Contoh: F_ aks 'T' FILE *fd; char aks; fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

94 Contoh: F_ aks 'T' FILE *fd; char aks; fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

95 Contoh: FT_ aks 'T' FILE *fd; char aks; fd = fopen("teks.dat", "r");
if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd Until ... FTSM 43600 UKM Bangi<eof> fail teks.dat

96 Contoh: FTSM 43600 UKM Bangi_ aks 'i' FILE *fd; char aks;
fd = fopen("teks.dat", "r"); if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

97 Contoh: FTSM 43600 UKM Bangi_ aks <eof> FILE *fd; char aks;
fd = fopen("teks.dat", "r"); if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

98 Contoh: FTSM 43600 UKM Bangi_ aks <eof> FILE *fd; char aks;
fd = fopen("teks.dat", "r"); if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

99 Contoh: FTSM 43600 UKM Bangi_ aks <eof> FILE *fd; char aks;
fd = fopen("teks.dat", "r"); if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

100 Contoh: FTSM 43600 UKM Bangi_ aks <eof> FILE *fd; char aks;
fd = fopen("teks.dat", "r"); if (fd != NULL) { aks = getc(fd); while (aks != EOF) { printf("%c", aks); } fclose(fd); fd FTSM 43600 UKM Bangi<eof> fail teks.dat

101 putc() putc() can be used to print one character into a file.
Example : FILE *fd; ... putc('X', fd);

102 Example : aks ??? fin ??? fout ??? fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout ??? FTSM 43600 UKM Bangi<eof> fail in.dat

103 Example : aks ??? fin fout ??? fail in.dat FILE *fin, *fout; char aks;
fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout ??? FTSM 43600 UKM Bangi<eof> fail in.dat

104 Example : aks ??? fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> <eof> fail out.dat fail in.dat

105 Example : aks ??? fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> <eof> fail out.dat fail in.dat

106 Example : aks ??? fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> <eof> fail out.dat fail in.dat

107 Example : aks 'F' fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> <eof> fail out.dat fail in.dat

108 Example : aks 'F' fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> <eof> fail out.dat fail in.dat

109 Example : aks 'F' fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> <eof> fail out.dat fail in.dat

110 Example : aks 'F' fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> F<eof> fail out.dat fail in.dat

111 Example : aks 'T' fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> F<eof> fail out.dat fail in.dat

112 Example : aks 'T' fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> F<eof> fail out.dat fail in.dat

113 Example : aks 'T' fin fout Until … fail out.dat fail in.dat
FILE *fin, *fout; char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout Until … FTSM 43600 UKM Bangi<eof> FT<eof> fail out.dat fail in.dat

114 Example : aks 'i' fin fout fail out.dat fail in.dat FILE *fin, *fout;
char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> FTSM 43600 UKM Bangi<eof> fail out.dat fail in.dat

115 Example : aks <eof> fin fout fail out.dat fail in.dat
FILE *fin, *fout; char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> FTSM 43600 UKM Bangi<eof> fail out.dat fail in.dat

116 Example : aks <eof> fin fout fail out.dat fail in.dat
FILE *fin, *fout; char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> FTSM 43600 UKM Bangi<eof> fail out.dat fail in.dat

117 Example : aks <eof> fin fout fail out.dat fail in.dat
FILE *fin, *fout; char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> FTSM 43600 UKM Bangi<eof> fail out.dat fail in.dat

118 Example : aks <eof> fin fout fail out.dat fail in.dat
FILE *fin, *fout; char aks; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { aks = getc(fin); while (aks != EOF) { putc(aks, fout); } fclose(fin); fclose(fout); fout FTSM 43600 UKM Bangi<eof> FTSM 43600 UKM Bangi<eof> fail out.dat fail in.dat

119 fgets() fgets() is used to read one string from a file.
Three parameter involved: - An array (use to allocate string) - Maximum number of character. - Pointer to a specific file.

120 fgets() Example: Read from a file pointed char str[20]; by fd
FILE *fd; ... fgets(str, 20, fd); Read from a file pointed by fd An array for character Maximum 19 characters

121 fgets() fgets() will return NULL if error was occurred or when it comes to the end of file.

122 nama ? Example: gred ??? matrik ??? fin ??? fail in.dat FILE *fin;
char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred ??? matrik ??? fin ??? FTSM 43600 UKM Bangi<eof> fail in.dat

123 nama ? Example : gred ??? matrik ??? fin fail in.dat FILE *fin;
char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred ??? matrik ??? fin Ahmad B Salim A Aminah A<eof> fail in.dat

124 nama ? Example : gred ??? matrik ??? fin fail in.dat FILE *fin;
char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred ??? matrik ??? fin Ahmad B Salim A Aminah A<eof> fail in.dat

125 nama ? Example : gred ??? matrik ??? fin fail in.dat FILE *fin;
char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred ??? matrik ??? fin Ahmad B Salim A Aminah A<eof> fail in.dat

126 nama ? Example : gred ??? matrik ??? fin fail in.dat FILE *fin;
char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred ??? matrik ??? fin Ahmad B Salim A Aminah A<eof> fail in.dat

127 nama 'A' 'h' 'm' 'a' 'd' '\0' ? Example : gred ??? matrik ??? fin fail
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred ??? matrik ??? fin Ahmad B Salim A Aminah A<eof> fail in.dat

128 nama 'A' 'h' 'm' 'a' 'd' '\0' ? Example : gred 'B' matrik 1223 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'B' matrik 1223 fin Ahmad B Salim A Aminah A<eof> fail in.dat

129 nama 'A' 'h' 'm' 'a' 'd' '\0' ? Example : gred 'B' matrik 1223 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'B' matrik 1223 fin B (1223) Ahmad _ Ahmad B Salim A Aminah A<eof> fail in.dat

130 nama 'S' 'a' 'l' 'i' 'm' '\0' ? Example : gred 'B' matrik 1223 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'B' matrik 1223 fin B (1223) Ahmad _ Ahmad B Salim A Aminah A<eof> fail in.dat

131 nama 'A' 'h' 'm' 'a' 'd' '\0' ? Example : gred 'A' matrik 3312 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 3312 fin B (1223) Ahmad _ Ahmad B Salim A Aminah A<eof> fail in.dat

132 nama 'A' 'h' 'm' 'a' 'd' '\0' ? Example : gred 'A' matrik 3312 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 3312 fin B (1223) Ahmad A (3312) Salim _ Ahmad B Salim A Aminah A<eof> fail in.dat

133 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? Example : gred 'A' matrik 3312 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 3312 fin B (1223) Ahmad A (3312) Salim _ Ahmad B Salim A Aminah A<eof> fail in.dat

134 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? Example : gred 'A' matrik 4021 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 4021 fin B (1223) Ahmad A (3312) Salim _ Ahmad B Salim A Aminah A<eof> fail in.dat

135 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? Example : gred 'A' matrik 4021 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 4021 fin B (1223) Ahmad A (3312) Salim A (4021) Aminah _ Ahmad B Salim A Aminah A<eof> fail in.dat

136 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? Example : gred 'A' matrik 4021 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 4021 fin NULL B (1223) Ahmad A (3312) Salim A (4021) Aminah _ Ahmad B Salim A Aminah A<eof> fail in.dat

137 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? Example : gred 'A' matrik 4021 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 4021 fin B (1223) Ahmad A (3312) Salim A (4021) Aminah _ Ahmad B Salim A Aminah A<eof> fail in.dat

138 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? Example : gred 'A' matrik 4021 fin
FILE *fin; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); if (fin != NULL) { while (fgets(nama, 10, fin) != NULL) { fscanf(fin, "%d %c\n", &matrik, &gred); printf("%c (%d) ", gred, matrik); printf("%s\n", nama); } fclose(fin); gred 'A' matrik 4021 fin B (1223) Ahmad A (3312) Salim A (4021) Aminah _ Ahmad B Salim A Aminah A<eof> fail in.dat

139 fputs() fputs() is used to print a string into a file. Example :
FILE *fd; char str[] = "Universiti Kebangsaan"; ... fputs(str, fd);

140 nama ? Example: gred ??? matrik ??? fin ??? fout ??? fail in.dat
FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("output", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin ??? fout ??? FTSM 43600 UKM Bangi<eof> fail in.dat

141 nama ? gred ??? matrik ??? fin fout ??? fail in.dat Example :
FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout ??? Ahmad B Salim A Aminah A<eof> fail in.dat

142 nama ? gred ??? matrik ??? fin fout fail out.dat fail in.dat Example :
FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout Ahmad B Salim A Aminah A<eof> <eof> fail out.dat fail in.dat

143 nama ? gred ??? matrik ??? fin fout fail out.dat fail in.dat Example :
FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout Ahmad B Salim A Aminah A<eof> <eof> fail out.dat fail in.dat

144 nama ? gred ??? matrik ??? fin fout fail out.dat fail in.dat Example :
FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout Ahmad B Salim A Aminah A<eof> <eof> fail out.dat fail in.dat

145 nama ? gred ??? matrik ??? fin fout fail out.dat fail in.dat Example :
FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout Ahmad B Salim A Aminah A<eof> <eof> fail out.dat fail in.dat

146 nama 'A' 'h' 'm' 'a' 'd' '\0' ? gred ??? matrik ??? fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout Ahmad B Salim A Aminah A<eof> <eof> fail out.dat fail in.dat

147 nama 'A' 'h' 'm' 'a' 'd' '\0' ? gred ??? matrik ??? fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout Ahmad B Salim A Aminah A<eof> Ahmad<eof> fail out.dat fail in.dat

148 nama 'A' 'h' 'm' 'a' 'd' '\0' ? gred ??? matrik ??? fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred ??? matrik ??? fin fout Ahmad B Salim A Aminah A<eof> Ahmad <eof> fail out.dat fail in.dat

149 nama 'A' 'h' 'm' 'a' 'd' '\0' ? gred 'B' matrik 1223 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'B' matrik 1223 fin fout Ahmad B Salim A Aminah A<eof> Ahmad <eof> fail out.dat fail in.dat

150 nama 'S' 'a' 'l' 'i' 'm' '\0' ? gred 'B' matrik 1223 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'B' matrik 1223 fin fout Ahmad B Salim A Aminah A<eof> Ahmad <eof> fail out.dat fail in.dat

151 nama 'S' 'a' 'l' 'i' 'm' '\0' ? gred 'B' matrik 1223 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'B' matrik 1223 fin fout Ahmad B Salim A Aminah A<eof> Ahmad Salim <eof> fail out.dat fail in.dat

152 nama 'S' 'a' 'l' 'i' 'm' '\0' ? gred 'A' matrik 3312 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'A' matrik 3312 fin fout Ahmad B Salim A Aminah A<eof> Ahmad Salim <eof> fail out.dat fail in.dat

153 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? gred 'A' matrik 3312 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'A' matrik 3312 fin fout Ahmad B Salim A Aminah A<eof> Ahmad Salim <eof> fail out.dat fail in.dat

154 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? gred 'A' matrik 3312 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'A' matrik 3312 fin fout Ahmad B Salim A Aminah A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat

155 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? gred 'A' matrik 4021 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'A' matrik 4021 fin fout Ahmad B Salim A Aminah A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat

156 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? gred 'A' matrik 4021 fin fout NULL
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'A' matrik 4021 fin fout NULL Ahmad B Salim A Aminah A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat

157 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? gred 'A' matrik 4021 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'A' matrik 4021 fin fout Ahmad B Salim A Aminah A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat

158 nama 'A' 'm' 'i' 'n' 'a' 'h' '\0' ? gred 'A' matrik 4021 fin fout fail
Example : FILE *fin, *fout; char nama[10], gred; int matrik; fin = fopen("in.dat", "r"); fout = fopen("out.dat", "w"); if (fin != NULL && fout != NULL) { while (fgets(nama, 10, fin) != NULL) { fputs(nama, fout); fputc('\n', fout); fscanf(fin, "%d %c\n", &matrik, &gred); } fclose(fin); fclose(fout); gred 'A' matrik 4021 fin fout Ahmad B Salim A Aminah A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat


Download ppt "File Manipulation Knowledge Understand the concept of file input and output Skills Able to write an application program involving input output files."

Similar presentations


Ads by Google