FILE PROCESSING
SUB TOPICS :
- Files and Streams
- File Definition
- Open File
- Close File
- Input File
- Output File
Streams Definition
•
To keep key in data from
keyboard need to be saved at secondary storage device as a data file.
• Stream is a sequence of character. All input and
output data is a stream. C sees file as a stream.
• When
a C program run, there are three (3) standard streams activated:
1. Standard Input Stream
Controlling input
stream from keyboard
2. Standard Output Stream
Controlling output
stream to the monitor
3. Standard Error Stream
Controlling the
error messaging
• Each stream associated with a file.
File Definition
• File is a collection of record
• Record is a collection of field
• Field is a block of byte
• Byte is collection of bit
• Opening a File using fopen():
FILE *fopen (const char *filename,
const char *mode );
• fopen() defined at <stdio.h>
• fopen() return a pointer to the start of a buffer area.
Null will be returned if file unable to open.
Mode Description
“r” opening
a file to be read.
“w” creating
a file to be written.
“a” opening
a File for data append.
“r+” opening
a File for read/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
int fclose (FILE *stream);
• fclose() defined at <stdio.h>
• fclose() will return 0 if successful, and EOF if error
• EOF (End Of File) equals to -1
• fclose() will release the buffer area and immediately send
the remaining data to file.
Input and Output File
• fgetc (INPUT)
• Read one character from a file
• fgetc(stdin) equivalent with getchar()
• Syntax : int fgetc( FILE *stream );
• Return the character when successful, and
EOF while error
• fputc (OUTPUT)
• Writing one character to a file
• fputc('a', stdout) similar with putchar( 'a' )
• Syntax: int fputc( int c, FILE *stream
);
• Return a character when successful, and
EOF if error
• fgets (INPUT)
• Syntax: char *fgets( char *string,
int n, FILE *stream );
• Read one line from a file that ended with
new line, or at maximum of n-1 number of character.
• Return a string if successful and NULL
while error
• fputs (OUTPUT)
• Writing a line to a file
• Syntax: int fputs( const char *string,
FILE *stream );
• Return non-negative value while successful
and EOF if error.
• fscanf
(INPUT)
– Syntax:
int fscanf( FILE *stream,
const char *format [, argument ]... );
– Read data from file inline with the scanf
formatting.
– Return the number of field read while
successful, and EOF if error
• fprintf (OUTPUT)
– Syntax:
int fprintf( FILE *stream,
const char *format [, argument ]...);
– Writing data to a file using the printf
format.
– Return number of byte written if
successful and negative value if error.
– fwrite
– syntax: size_t fwrite( const void *buffer,
size_t size, size_t count, FILE *stream );
– Writing a block of data in the buffer area
to the file
– Return number of byte data written, and
error otherwise.
– fread
– Syntax: size_t fread( void *buffer,
size_t size, size_t count, FILE *stream );
– Read a block size of data from a file
– feof
– Syntax : int feof( FILE *stream
);
– Finding out if the pointer has reached
end-of-file
– Return 0 if not end-of-file
Summary
– Stream is a sequence of character.
All input and output data is a stream. C sees file as a stream.
– File Definition
–
File
is a collection of record
–
Record
is a collection of field
–
Field
is a block of byte
–
Byte
is collection of bit
2201760945
Skyconnectiva
Kevin Orlando Sutanto
Komentar
Posting Komentar