pure C I/O

this is a review from GNU C programming

stream

two basic mechanism for representing the connection between your program and the file: streams & file descriptors. FD is represented as objects of type int; streams are represented as FILE * objects

a stream is an abstract concept reprensenting a communication channel to a file, a device, or consider it as a sequence of characters with functions to take characters out of one end, and put characters into the other end, like a pipe.

file position

an integer representing the number of bytes from the beginning of the file; each time a character read or written, the file position is incremented, namely, access to a file is sequential.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
long int ftell(FILE \* stream); //return file position
int fseek(FILE \*stream, long int offset, int whence);
/\* whence is SEEK\_SET | SEEK\_CUR | SEEK\_END \*/
```
“write to a file” is always appended sequentially to the end of the file, regardless of the file position; but “read from a file” is used the current file position. which means multiple reading can happens simultaneously with an independent file pointer. In fact, each opening creates an independent file position pointer, even in the same program to open a file twice.
## std streams
```c
FILE *stdin ;
FILE *stdout;
FILE *stderr;
FILE *fopen(const char *filename, const char *openMode);
/* create a new stream connected to the file */
int fclose(FILE *stream)
/* disconnected between the stream and the file, any buffered output is written and any buffered input will discarded */

ASCII IO

1
2
3
4
5
6
7
8
9
int fputs(const char *s, FILE *stream) ;
/* write a char array into stream. this call doesn't add a newline or terminal null character */
char *fgets(char *s, int n, FILE *stream);
/* read n number of char array from stream, default add a newline character */
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
/* read a whole line from stream */

block IO

usually block stands for either block data or text in fixed-size, instead of characters or lines

1
2
3
4
5
size_t fread(void *data, size_t size, size_t n, FILE *stream);
/* read #n block, each block has size from stream, and store in data */
size_t fwrite(const void *data, size_t size, size_t n, FILE *stream);
/* write #n block, each block has size from buffer data to stream */

formatted IO

1
2
3
4
5
6
7
int printf(const char *template, …); //write to std output
int fprintf(FILE *stream, const char *template, ...); write to stream
int sprintf(char *s, const char *template, ...); //write to a string
int scanf(const char *template, …) //formatted read from stdin
int fscanf(FILE *stream, const char *template, …) // read from stream
int sscanf(const char *s, const char *template, …) // read from a string

EOF

1
2
3
4
int feof(FILE* stream) ;
/* return nonzero iff end of file indicator is set for stream */
int ferror(FILE* stream) ;
/* return nonzero iff error indicator is set for stream */

stream buffer

stream and file is not communicated character-by-character, there is a buffer for I/O.

1
2
int fflush(FILE *stream)
/* take any buffered output on stream to be delivered to the file */

file descriptor

1
2
3
4
5
6
7
8
9
10
11
12
int open(const char *filename, int flags);
int open64(const char *filename, int flags) ;
// allow large file mode
size_t read(int fd, void *buffer, size_t size)
// read from fd size bytes into buffer
size_t pread(int fd, void *buffer, size_t size, off_t offset)
// start reading from position “offset”
size_t write(int fd, const void *buffer, size_t size)
off_t lseek(int fd, off_t offset, int whence)
// change the file position of fd
FILE *fdopen(int fd, const char *opentype)
// return a new stream for this fd

synchronizing I/O

1
2
3
4
void sync(void) ;
// to ensure all operations finished before they return
int fsync(int fd) ;
// to ensure all data associated with the fd is written to the device

not yet

async I/O, event I/O, interrupt driven I/O, GPIO …