- CPSC 461: Copyright © 2002 Katrin Becker 1998-2002 Last Modified May 20, 2000 01:49 PM
What are the basic file operations and structure?
physical VS logical files
2 main points of view...
- physical
- logical
- - may be very different
-
logical = how we (as programmers) see the file
- - usually thought of as being in one piece
- - can be anything (ASCII, pictures, records, music)
- - sub-structure imposed by software that is uses
-
physical = how O/S sees the file (in UNIX this is always just a stream of bytes)
- - organization of actual bytes on storage medium may be quite different from logical organization
- - It's important to remember the distinction between the physical files and devices and the logical structure of the same thing.
To access: (every language has some way to accomplish these:)
- need to establish connection between logical and physical file
- need to 'announce' our intentions (read/write/new/append/etc.)
- sometimes, need to position the file pointer
- do the read & write operations
- close the file
So, what are the fundamental operations done on files?
Basic File Operations in C: (see text for C++ routines)
- FILE structure defined in stdio ; keeps current position, end of file location, location in memory of associated buffer, etc.
-
- opening files :
- FILE *fopen(const char *file_id, const char *mode)
- file_id : pathname of file
- mode : describes how file will be used
- "r" = read; "w" = write; "a" = append (create file if it doesn't exist); "+" after mode means read and write but must execute fseek before switching from read to write or vice-versa.
- Returns pointer to file structure that holds file description
-
- closing files :
- int fclose(FILE *file);
- file : pointer to file structure
- Returns 0 if successful; EOF if not
- seeking :
- int fseek(FILE *file, long offset, int origin)
- file : pointer to file descriptor
- offset : longint = # of bytes to move from origin, can be negative
- origin : SEEK_SET (0) = from beginning of file
- SEEK_CUR (1) = from current position
- SEEK_END (2) = from end of file
- WARNING: C doesn't stop you from positioning the pointer beyond the end or beginning of the file.
- Returns 0 if OK, non-zero if not.
- special characters: ^z; CR; LF
- ^z : typical end-of-file marker in DOS
- UNIX just keeps track of file size and current position
- CR/LF : end-of-line pair
- UNIX uses NL (ASCII 10)
-
- reading:
- size_t fread (const void *location, size_t bytes, size_t items, FILE *file)
- location : where in memory to put stuff read
- bytes : # of bytes in each item
- items : # of items to read
- file : pointer to file descriptor
- Returns # items or partial items actually read.
- WARNING: make sure you have enough space at location; C doesn't care.
-
- writing:
- size_t fwrite (const void *location, size_t bytes, size_t items, FILE *file)
- location : where in memory to get stuff to write
- bytes : # of bytes per item
- items : # of items to write
- file : pointer to file descriptor
Returns # items or partial items actually written.
- UNIX directory structure:
- physical VS logical files
- again how we see directory vs. how UNIX sees it
- to UNIX each file is merely a sequence of bytes
- STDIN, STDOUT, STDERR as files
- I/O redirection and pipes
- basically sets up switches between STDIN, STDOUT, and regular files
-
CPSC 461: Copyright © 2002 Katrin Becker 1998-2002 Last Modified May 20, 2000 01:49 PM