File Processing

File Processing

File Definition

File is a collection of record (Kumpulan baris)

Record is a collection of field

Field is a block of byte

Byte is collection of bit

Byte terdiri dari 8 bit

3 macam stream:

-) Standard input stream (stdin) : Controlling input stream from keyboard

-) Standard ouput stream (stdout) : Controlling output stream to the monitor

-) Standard error stream (stderr) : Controlling the error messaging

Text file saved in a text format or ASCII

Storage size data: 10.000 needs 5 byte

Orc:> TYPE = file name

Binary File

hanya bisa dibuka data sendiri

Buffer Area

Syntax:

FILE*FP;

Open File

Opening a File using fopen():

FILE *fopen (const char *filename, const char *mode );

fopen() defined at <stdio.h>

“r” (opening a file to be read)

“w” (creating file to be written)

“a” (opening a file for data append)

“r” (opening a file for read or write)

w+ (creating file for read/write)

a+” (opening a File for read/append)

“rb” (opening a File (binary) to be read)

“wb” (creating a file (binary) for write operation)

 

Close File

Closing a file using fclose( ):

Int fclose (File*stream) :

f close. ( ) definied at <stdio.h> (menutup file)

 

f close (m ) will return o if succesful and EOF if error

EOF (End Of File) equals to -1

f close ( ) will release the buffer area and send the remaining data file

Closing a File using fcloseall():

int fcloseall (void);

Close all active stream except: stdin, stdout, stdprn, stderr, and stdaux.

Will return the number of stream closed if successful, and return EOF instead.

Header file <stdio.h>

 

input and ouput file

f gets (Input)

f puts(Output)

f scanf (Input)

f printf (output)

f write

f read

feof

  • Example using fwrite():

fwrite( &mhs, sizeof( mhs ), 1, fp );

  • &mhs = data origin location
  • sizeof(mhs) = return the size of mhs
  • 1 => one time write sizeof(mhs)
  • fp = file pointer

 

Function And Recursion

Function and Recursion

-) Modular Progamming

Program teerdapat di dalam modul

Modul dijadikan sebagai fungsi

Function disebut juga modal

Tujuan Parameter untuk berkomunikasi dengan fungsi lain

Library US user dibagi 2:

-) Libary function

Used definied function

Library function

Example: clrsrc ( ) in conio.h

Sqrt ( ) in math.h

Function definition

Function Construction

Return value type function name ( parameter list )

{

Statements:

}

Contoh parameter

Int maximum ( int x, int y) {

Int max = x;                  ( int harus di atas fungsi main )

If ( y > max ) max; y;

Return max

}

Fungsi prototype

Syntax : return value type function name

( Parameter List );

Int          maximum ( int a; int b; )

Identifier Scoping

Variabel global : bisa pakai semua fungsi

Variabel lokal   : Cuma fungsi di main

 

Recursive Function (2)

-) Base case : return value with out calling next recursive call

-) Reduction step : sequence of input value converging

Review Sebelum Quiz

REVIEW SEBELUM QUIZ

3 langkah sistem pemograman :

-) Input

-) Process

-) Output

Input adalah tahap proses suatu data yang dimasukkan

Process adalah tahap pemrosesan suatu data

Output adalah hasil dari suatu data yang di input & process

Yang termasuk dalam input:

-) If

-) Else

-) Else if

-) Nested if

-) Switch Case

-) While

-) Do While

-) For

Yang termasuk dalam process:

-) Selection

-) Looping

-) Storage

Coding Segitiga Bolong

#include <stdio.h>

 

int main(){

int i, j, k, baris=20;

baris /= 2;

for(i=0; i<baris; i++){

for(j=-(baris-1); j<baris; j++){

if(j<0){

k = j*(-1);

}

else{

k = j;

}

if(i==k||i==(baris-1)){

printf(“*”);

}

else{

printf(” “);

}

}

printf(“\n”);

}

for(i=(baris-2); i>=0; i–){

for(j=-(baris-1); j<baris; j++){

if(j<0){

k = j*(-1);

}

else{

k = j;

}

if(i==k){

printf(“*”);

}

else{

printf(” “);

}

}

printf(“\n”);

}

return 0;

}