File Processing
File Processing
File and Streams
Streams => To keep key in data from keyboard need to be saved at secondary storage device as a data file.
=> A suquence of character.
=> All input and output data is a stream.
There are 3 standard streams :
1) Standard Input Stream : Controlling input stream for keyboard
2) Standard Output Stream : Controlling output stream for keyboard
3) Standard Error Stream : Controlling the error messaging
File => A collection of record
=> Record is a collection of field
=> Field is a block of byte
=> Byte is collection of bit
• TEXT FILE saved
in a text format or ASCII File
– Storage
size depends on its data: 10000 needs 5 byte
– Can
be open using standard text editor application
– or
c:>TYPE file_name
• BINARY FILE storing
numerical data in affixed format in line with micro-processor format definition
(example: format sign-magnitude 2’s complement)
Buffer Area
Part of memory used as a temporary space before data moved to a file.
Syntax :
FILE
*fp;
Open File
Opening
a File using fopen() :
FILE *fopen (const char *filename,
const char *mode );
fopen() : return
a pointer to the start of a buffer area. Null will be returned if file unable
to open.
Possible mode Value :
“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
Closing a File using fclose() :
int fclose
(FILE *stream);
fclose() : will release the buffer area and immediately send the remaining data to file.
Closing a File using fcloseall() :
int
fcloseall (void);
fcloseall() : will return the number of stream closed if successful and return EOF instead.
Input & Output File
~ fgetc <input>
- Read one character from a file
- fgetc(stdin) = getchar()
- Syntax : int fgetc( FILE * stream );
- Return the character when successful and EOF while error.
~ fputc <ouput>
- Writing one character to a file
- fputc('a', stdout) similiar 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
Comments
Post a Comment