cmpsc 311 test 2 IO Flashcards
fopen() syntax
FILE *fopen(const char *path, const char *mode);
IO, pointer
the fopen function opens a file for — and returns a —- to a FILE* structure
stream
A FILE* structure is also referred to as a —-
r
–: open text file for reading. The stream is positioned at the beginning of the file
r+
— Open for reading and writing. The stream is positioned at the beginning of the file
w
–: truncate file to zero length or create text file for writing. the stream is positioned at the beginning of the file
w+
—: Open for reading and writing. The file is created if it does not exist, otherwise it is truncated
a
Open for appending (writing at the end of file). The file is created if it does not exist
a+
Open for reading and appending (writing at end of file). the file is created if it does not exist
fscanf, fgets
there are two dominant ways to read the file: ——- and ——
fscanf, scanf
— reads the data from the file just like —–, just reading and writing
fgets
— reads a line of text from the file
fprintf, fputs
there are two dominant ways to write the file, —- and —-
fprintf, printf
— writes data to the file just like —-, just reading and writing
fputs
—- writes a line of text to the file
FILE*
—- based IO is buffered
fflush
—- attempts to reset/the flush state
int fflush(FILE *stream)
fflush syntax
buffered, written, pushed, OS/disk
FILE* based writes are —–, so there may be data ——-, but not yet —– to the —/—–
fflush()
—– forces a write of all buffered data
buffered, current, current
FILE* based reads are —-, so the — data (in the process space) may not be —-
fflush()
—– discards buffered data from the underlying file
NULL, fflush, all
If the stream argument is —–, ——() flushes —- open output streams
fclose()
—– closes the file and releases the memory associated with the FILE* structure
storage
fclose() implicitly flushes the data to ——
file, integer file handle
the open function opens a —- for IO and returns an —— —— —–
int open(const char *path, int flags, mode_tmode);
open() syntax
path
open():
— is a string containing the absolute or relative path to the file to be opened
flags
open():
— indicates the kind of open you are requesting
mode
open():
—- sets a security policy for the file
file handle
open() returns a —- —-
O_RDONLY
open() flags:
—- read only
O_WRONLY
open() flags:
—- write only
O_RDWR
open() flags:
—- read and write
O_CREAT
open() options:
—- if the file does not exist it will be created
O_EXCL
open() options:
— ensure that this call creates the file, fail otherwise (fail if already exists)
O_TRUNC
open() options:
— if the file already exists it will be truncated to length 0
bitwise or (|)
You —— —- the mode/options you want in open()
discretionary access control, user
The UNIX filesystem implements —– —– —- through file permissions set by user. The permissions are set at the discretion of the —
bits, access
Every file in the file system ha has a set of —– which determine who has —– to the files.
user
—— the owner is typically the creator of the file, and the entity in control of the access control policy
group
—- a set of users on the system setup by the admin
world
—– the set of everyone on the system
root
Not discretionary access control can be overridden by the —- user
READ
3 rights in UNIX filesystem:
—-: allows the subject(process) to read the contents of the file
WRITE
3 rights in UNIX filesystem:
—-: allows the subject (process) to alter the contents of the file
EXECUTE
3 rights in UNIX filesystem:
—: allows the subject (process) to execute the contents of the file (eg, shell program, executable, …)
r, w, x, -
UNIX Access policy:
Really, this is a bit string encoding an access policy.
And a policy is encoded as —, —-, — if enabled and — if not
rwxrw—x
encoding syntax example. says user can read, write and execute, group can read and write, and world can execute only
S_IRWXU 00700
Specify a file access policy by bit-wise ORing(|):
——–: user (file owner) has read, write and execute
S_IRUSR 00400
Specify a file access policy by bit-wise ORing(|):
——–: user has read permission
S_IWUSR 00200
Specify a file access policy by bit-wise ORing(|):
——–: user has write permission
S_IXUSR 0100
Specify a file access policy by bit-wise ORing(|):
——–: user has execute permission
S_IRGRP 00040
Specify a file access policy by bit-wise ORing(|):
——–: group has read permission
S_IWGRP 00020
Specify a file access policy by bit-wise ORing(|):
——–: group has write permission
S_IXGRP 00010
Specify a file access policy by bit-wise ORing(|):
——–: group has execute permission
S_IRWXO 00007
Specify a file access policy by bit-wise ORing(|):
——–: world has read, write, and execute permission
S_IROTH 00004
Specify a file access policy by bit-wise ORing(|):
——–: world has read permission
S_IWOTH 00002
Specify a file access policy by bit-wise ORing(|):
——–: world has write permission
S_IXOTH 00001
Specify a file access policy by bit-wise ORing(|):
——–: world has execute permission
S_IRWXG 00070
Specify a file access policy by bit-wise ORing(|):
——–: group has read, write, and execute permission
int
Why is an —- returned by open() file
file descriptor
a —– —– is an index assigned by the kernel into a table of file information maintained in the OS.
unique, process, open
The file descriptor table is —– to each —– and contains the details of —- files
calling IO system
File descriptors are used to reference when —– the —– —— calls
kernel, process, system call
The —- accesses the file for the —– and returns the results in —– — response
primitive
—— reading and writing mechanisms that only process only blocks of opaque data
ssize_twrite(int fd, const void *buf, size_tcount);
primitive write syntax
ssize_tread(int fd, void *buf, size_tcount);
primitive read syntax
fd, buf, count
primitive read/write syntax:
Where — is the file descriptor, —- is an array of bytes to write from or read into, and —- is the number of bytes to read or write
number, bytes, result
both read() and write(0 returned the —- of —— read and written. Be sure to always check the —-
buffer, output
On reads, you are responsible for supplying a —– that is large enough to put the —– into
close(), -1
—– closes the file and deletes the file’s entry in the file descriptor table. Always reset your file handles to —- to avoid use after close
fopen
—- provides you with buffering IO that may or may not turn out to be a faster than what you’re doing with open
fopen, binary mode
—– does line ending translation if the file is not opened in —– —, which can be very helpful if your program is ever ported to a non-UNIX environment`
FILE*, fscanf, stdio
A —- gives you the ability to use —- and other — functions that parse out data and support formatted output
FILE*, file handle
IMO: use —– style I/O for ASCII processing, and —- —— I/O for binary data processing
include, stdio.h, sys/types.h, sys/stat.h, fcnt1.h, unistd.h
each of the styles of I/O requires a different set of —– files.
file handle I/O requires: