Files in C

Files

Devices represented as files
If file can be physical device, not fixed in size or behavior

Streams are associated with files
May support file position indicator [0,length]
Binary or not
Can be closed, opened, flushed
Can be buffered, unbuffered, line buffered
Unbuffered - i/o immediate
Fully buffered - i/o accumulated into block, then passed
Line buffered - block size depends on \n

Each file opened has a file descriptor (fd)
fd describes state of file
Opened, closed, position etc

FILE --- a struct defined in stdio.h
Functions
-fopen (const char* path, const char* mode);
-fscanf(stdin,....) == scanf
-fprintf(stdout,....) == printf
-fread
-fwrite
-fclose
-fgets
-feof(stream), does not happen until beyond stream

When program begins, special files
-stdin, stdout, stderr are opened
If file position supported, position == 0
Print, scan adjusts position in stream
Get position == ftell, change position == fseek

Setvbuf defines how a stream should be buffered
int setvbuf (FILE* stream, char* buffer, int mode, size_t size)
Modes
- _IOFBF -- full
- _IOLBF -- line
- _IONBF -- no buffering
Size
Buffer size in bytes

Comments

Popular Posts