Professional Documents
Culture Documents
The C Programming Language: Facultatea de Matematica Si Informatica
The C Programming Language: Facultatea de Matematica Si Informatica
FCB2
Communication channel 2
n2=open(file_to_write)
...
write(n2,..)
...
read(n1,..)
...
Communication: if(eof)
close(file_to_read)
- data (what is being transfered) ...
- control (how the transfer takes place)
Memory
File Control Block (FILE structure)
Program
fp1 = fopen(file_to_read)
I/O device 1
... Communication channel 1
fread(fp1,..)
...
(stream)
fread(fp1,..)
...
if(eof)
fclose(fp1)
...
Memory
Program
keyboard
file1
stdout
file2
stderr
display
*Because the FCB is stored in user memory, as opposed to system meory in the UNIX I/O model!
Any uninitialized pointer might give un-authorized access to the FCB and compromize the data transfer...
2. file access: acces data “inside” a file (apply also to physical devices, not only disk files!)
- open stream
- manage buffers
- transfer data
- test for potential errors
- handle errors
- get/set position
- close stream
Binary file: collection of bytes , which represent data (or part of some data!) stored in internal
representation. No extra characters are stored in binary files! This is consistent across all
Operating Systems, except the eventual filling with 0 at the end of the file under some
OSs.
Consequence:
- the text or binary mode should be indicated when opening
- there are specific functions for transfering data to/from a file, according to the mode in
which it was opened
Prototype:
FILE *fopen(char *file_name, char * mode);
FILE – struct type which contains all info needed to control the stream, including the buffers
Text mode
"r" "rt" - open text file in read mode
"w" "wt" - open text file in write mode
"a" "at" - open text file in append mode
"r+" "rt+" "r+t" - open text file in read mode for updating
"w+" "wt+" "w+t" - open text file in write mode for updating
"a+" "at+" "a+t" - open text file in append mode for updating
Binary mode
fflushall
setbuf
setvbuf
Flags are used to indicate the change (or lack of change) of some state. Examples: the error-flag, eof-flag.
Usually the flags are cleared (set to 0). When the state changes (an error occurs or end-of-file is reached after a transfer
function is called the flag is set (1). Testing the flags is possible (and should be done!) by calling specialized functions
(ferror(), feof()) which return the flags value.
Each error is associated to some error code. Whenever an error occurs, the coresponding error-code is placed into a
global variable (errno) which may be used in order to take some action (inform the user about the kind of error)
E.g.
function(fp); /* I/O function which might fail; failure will set the error flag
if(ferror(fp)) /* test the relevant flag for stream fp ; test() might be ferror() or feof()*/
/*handle error; this should include the usage of the current value of errno */
clearerr(fp); /* clear the error flag for the stream to be able to catch next error!*/
...
Variable length
args list