File Processing (5 December 2018)
What is Stream?
A sequence of character including Input, Output and even file.
There are 3 standard streams activated when a C program running :
A sequence of character including Input, Output and even file.
There are 3 standard streams activated when a C program running :
- Standard Input Stream : Keyboard
- Standard Output Stream : Monitor
- Standard Error Stream : Error Message
What is File?
File is a collection of record which is a collection of field of block of byte that is a collection of bit.
- Text file can be saved as a text format or ascii file
- Binary file storing numerical data in affixed format
What is Buffer?
A part of the memory used as a temporary storage before a data moved to a file that also can be called as stream pointer.
How to use buffer : FILE *fp;
fp is a file pointer pointing to the start of the buffer area.
How to open a file?
Inside <stdio.h> there is fopen() that can be used to open a file.
Can write it as FILE *fopen (const char *filename, const char *mode);
List of mode value:
- "r" : open and read file
- "w" : create and write a file
- "a" : open a file for data append
- "r+" : open and read or write the file
- "w+" : create and read or write a file
- "a+" : open and read or append a file
- "rb" : open and read a binary file
- "wb" : open and write a binary file
How to close a file?
Inside <stdio.h> there is fclose() that can be used to close a file.
Can write it as int fclose (FILE *stream);
It will return 0 if successful, and EOF if error which equals to -1
Also can use fcloseall() inside <stdio.h> to close all active stream except stdin, stdout, stdprn, stderr, and stdaux.
Can write it as int fcloseall(void);
It will return the number of stream closed if successful, and return EOF instead.
How to input and output a file?
- fgetc(INPUT)
- fputc(OUTPUT)
- fgets(INPUT)
- fputs(OUTPUT)
- fscanf(INPUT)
- fprintf(OUTPUT)
- fwrite
- fread
- feof
Komentar
Posting Komentar