File Manipulation Knowledge Understand the concept of file input and output Skills Able to write an application program involving input output files
Introduction A file is a collection of data. A file is a collection of related data stored in an auxiliary storage device.
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.
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().
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.
Input Output File Example: Fail ini adalah fail teks!<newline>Ia terdiri daripada dua baris.<newline><eof>
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
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.
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)
fopen() Example: fopen("input.dat", "r") fopen("atur.c", "w") fopen("output", "a")
fopen() Example: fopen("input.dat", "r") fopen("atur.c", "w") fopen("output", "a") Open file named input.dat for read only
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)
fopen() Contoh: fopen("input.dat", "r") fopen("atur.c", "w") fopen("output", "a") Open file named output Write only (start at the end file)
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.
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
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 ??? 34 23 43 54 76 97 48 22 63 91 33 input.dat
fopen() Example : FILE *fd; int data; fd = fopen("input.dat", "r"); fscanf(fd, "%d", &data); data ??? 34 23 43 54 76 97 48 22 63 91 33<eof> input.dat
fopen() Example : FILE *fd; int data; fd = fopen("input.dat", "r"); fscanf(fd, "%d", &data); data 34 34 23 43 54 76 97 48 22 63 91 33<eof> input.dat
fopen() We can also used the return value to check whether a file was successfully opened or not.
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 23 43 54 76 97 48 22 63 91 33 input.dat
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 23 43 54 76 97 48 22 63 91 33<eof> input.dat
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 23 43 54 76 97 48 22 63 91 33<eof> input.dat
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 34 23 43 54 76 97 48 22 63 91 33<eof> input.dat
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.
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 ??? _
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 ??? _
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 ??? _
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
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
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.
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 34 23 43 54 76 97 48 22 63 91 33<eof> input.dat
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 34 23 43 54 76 97 48 22 63 91 33<eof> input.dat
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).
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 ??? 34 23 43 54 76 97 48 22 63 91 33 fail kuasa2
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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).
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 ??? 34 23 43 54 76 97 48 22 63 91 33 fail input.dat
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
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
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
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
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
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
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
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
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: 1111 Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C Matrik: 1120 Gred: B _ fd Until ... 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C Matrik: 1120 Gred: B Matrik: 1123 Gred: B Matrik: 1124 Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C Matrik: 1120 Gred: B Matrik: 1123 Gred: B Matrik: 1124 Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C Matrik: 1120 Gred: B Matrik: 1123 Gred: B Matrik: 1124 Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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: 1111 Gred: C Matrik: 1120 Gred: B Matrik: 1123 Gred: B Matrik: 1124 Gred: A _ fd 4 1111 C 1120 B 1123 B 1124 A<eof> fail input.dat
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.
fscanf() Example: m ??? FILE *fd; int m; char g; 34 23 43 54 76 97 48 22 63 91 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 ???
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
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
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
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
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
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: 1111 Gred: C _ fail input.dat fd
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: 1111 Gred: C _ fail input.dat fd
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: 1111 Gred: C Matrik: 1120 Gred: B _ fail input.dat fd Until ...
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: 1111 Gred: C Matrik: 1120 Gred: B Matrik: 1123 Gred: B Matrik: 1124 Gred: A _ fail input.dat fd
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: 1111 Gred: C Matrik: 1120 Gred: B Matrik: 1123 Gred: B Matrik: 1124 Gred: A _ fail input.dat fd EOF
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: 1111 Gred: C Matrik: 1120 Gred: B Matrik: 1123 Gred: B Matrik: 1124 Gred: A _ fail input.dat fd
Another useful function for Input Output file Read/write function for Single character - getc() - putc() Read/write function for string fgets() fputs()
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);
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
putc() putc() can be used to print one character into a file. Example : FILE *fd; ... putc('X', fd);
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
fgets() fgets() will return NULL if error was occurred or when it comes to the end of file.
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
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
fputs() fputs() is used to print a string into a file. Example : FILE *fd; char str[] = "Universiti Kebangsaan"; ... fputs(str, fd);
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
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 1223 B Salim 3312 A Aminah 4021 A<eof> fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad<eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat
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 1223 B Salim 3312 A Aminah 4021 A<eof> Ahmad Salim Aminah <eof> fail out.dat fail in.dat