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

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
File I/O.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
F28PL1 Programming Languages Lecture 7 Programming in C - 2.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
File Handling Spring 2013Programming and Data Structure1.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Files A collection of related data treated as a unit. Two types Text
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
University of Washington Computer Programming I
EKT120: Computer Programming
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
What you need for the 1st phase of project
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
Character & String Knowledge
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Programming and Data Structure
File Input and Output.
File Handling.
Fundamental of Programming (C)
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
Files.
C Preprocessing File I/O
File Handling in C.
EPSII 59:006 Spring 2004.
FILE handeling.
ETE 132 Lecture 8 By Munirul Haque.
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
File I/O.
EECE.2160 ECE Application Programming
Files Chapter 8.
Presentation transcript:

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