Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

PhysicaI FiIes and LogicaI FiIes

Terms
physicaI fiIe
A file as seen by the operating system, and which actually exists on
secondary storage.
IogicaI fiIe
A file as seen by a program.
Remarks
O Programs read and write data from logical files.
O Before a logical file can be used, it must be associated with a physical file.

O This act of connection is called "opening" the file.
O Data in a physical file is persistent.
O Data in a logical file is temporary.
O A logical file is identified (within the program) by a program variable or
constant.
O The name and form of the physical file are dependent on the operating
system, not on the programming language.
Opening FiIes
Terms
open
To associate a logical program file with a physical system file.
fiIe descriptor
A cardinal number used as the identifier for a logical file by operating
systems such as UNX and PC-DOS.
Remarks
O The name of the physical file must be supplied to an open function.
O The open function must also be supplied with an access mode.
O The open function can also be supplied with a protection mode.
Access Mode
Terms
access mode
The type of (file) access allowed.
Remarks
O The access mode has several aspects:
o s the file is to be accessed by reading, by writing, or by both?
o What should be done with existing contents of the file?
o Should a new file be created if none exists?
o Should any character translation be done?
Protection Mode
Terms
protection mode
The security status of a file, defining who is allowed to access a file, and
which access modes are allowed.
Remarks
O Supported protection modes depend on the operating system, not on the
programming language.
O DOS supports protection modes of:
o Read only
o Hidden
o System
for all uses of the system.
O UNX supports protection modes of:
o Readable
o Writable
o Executable
for users in three categories:
o Owner (Usually the user who created the file)
o Group (members of the same group as the owner)
o World (all valid users of the system)
O Windows supports protection modes of:
o Readable
o Modifiable
o Writable
o Executable
for users which can be designated individually or in groups.:
CIosing FiIes
Terms
cIose
To disassociate a logical program file from a physical system file.
Remarks
O Closing a file frees system resources for reuse.
O Data may not be actually written to the physical file until a logical file is
closed.
O A programs should close a file when it is no longer needed.
Reading and Writing
Terms
read
To tranfer data from a file to program variable(s).
write
To tranfer data to a file from program variable(s) or constant(s).
Remarks
O The read and write operations are performed on the logical file with calls to
library functions.
O For read, one or more variables must be supplied to the read function, to
receive the data from the file.
O For write, one or more values (as variables or constants) must be supplied to
the write function, to provide the data for the file.
O For unformatted transfers, the amount of data to be transferred must also be
supplied.
End of FiIe Detection
Terms
end-of-fiIe
A physical location just beyond the last datum in a file.
Remarks
O The acronym for end-of-file is EOF.
O When a file reaches EOF, no more data can be read.
O Data can be written at or past EOF.
O Some access methods set the end of file flage after a read reaches the end
of file position.
O Other access methods set the end of file flage after a read attempts to read
beyond the end of file position.
Seeking
Terms
seek
To move to a specified location in a file.
byte offset
The distance, measured in bytes, from the beginning.
Remarks
O Seeking moves an attribute in the file called the file pointer.
O C++ library functions allow seeking.
O n DOS, Windows, and UNX, files are organized as streams of bytes, and
locations are in terms of byte count.
O Seeking can be specified from one of three reference points:
o The beginning of the file.
o The end of the file.
o The current file pointer position.
SpeciaI Characters in FiIes
Remarks
O Specifics of files can vary with the operating system.
O The C++ language was written originally for the UNX operating system.
O UNX and DOS (Windows) systems handle separators between lines
differently.
O n UNX files, lines are separated by a single new line character (ASC line
feed.)
O n DOS (Windows) files, lines are separated by a two characters (ASC
.,rri,e return and line feed.)
O When DOS files are opened in text mode, the internal separator ('\n') is
translated to the the external separator (<CR><LF>) during read and write.
O When DOS files are opened in -in,ry mode, the internal separator ('\n') is
not translated to the the external separator (<CR><LF>) during read and
write.
O n DOS (Windows) files, end-of-file can be marked by a "control-Z" character
(ASC $&).
O n C++ implementations for DOS, a control-Z in a file is interpreted as end-
of-file.
O Other operating systems may handle line separation and end-of-file
differently.
O mplementations of C++ should treat text files so that the internal
representations are the same as UNX.
O On UNX systems, files opened in text mode or binary mode behave the
same way.


C++ FiIe Support
Terms
access mode
The type of (file) access allowed.
protection mode
The security status of a file, defining who is allowed to access a file, and which access
modes are allowed.
Remarks
O C++ supports file access on three levels:
o Unbuffered, unformatted file access using handles.
o Buffered, formatted file access using the FLE structure.
o Buffered, formatted file access using classes.
O n C++, a file is opened by using library functions.
O The access mode has several aspects:
o s the file is to be accessed by reading, by writing, or by both?
o What should be done with existing contents of the file?
o Should a new file be created if none exists?
o Should any character translation be done?
FiIe Access with HandIes
Terms
fiIe descriptor
A cardinal number used as the identifier for a logical file by operating systems such as
UNX and PC-DOS.
Remarks
O For handle level access, the logical file is declared as an int.
O The handle is also known as a file des.riptor.
O Opening
O The C++ open function is used to open a file for handle level access.
O The handle open function must be supplied with (as arguments):
o The name of the physical file
o The access mode
o For new files, the protection mode
O The value returned by the open is the handle, and is assigned to the file variable.
O Prototypes:
O
O int open (const char Filename, int Access);
O int open (const char Filename, int Access, int Protection);
O Example:
O
O int Input;
O Input = open ("Daily.txt", J_RDJNLY);
O The following flags can be bitwise ored together for the access mode:
O_RDONLY
Read only
O_WRONLY
Write only
O_RDWR
Read or write
O_BINARY
Binary mode
O_TEXT
Text (ASC) mode
O_CREAT
Create file if it does not exist
O_EXCL
Do not open existing file (used only with O_CREAT)
O_APPEND
Leave any existing file contents and set file pointer at end of file
O_TRUNC
Delete any prior file contents
O The following flags can be bitwise ored together for the protection mode (which is
required if the O_CREAT flag is set):
S_IREAD
Read permission
S_IWRITE
Write permission
O 48ing
O The C++ .lose function is used to close a file for handle level access.
O The handle close function must be supplied with (as an argument):
o The handle of the logical file
O The value returned by the .lose is 0 if the close succeeds, and -1 if the close fails..
O Prototypes:
O
O int close (int Handle);
O Example:
O
O close (Input);
O #eading
O The C++ re,d function is used to read data from a file for handle level access.
O The handle read function must be supplied with (as an arguments):
o The handle of the logical file
o The address of the buffer into which the data will be read
o The number of bytes to be read
O The value returned by the re,d function is the number of bytes read.
O Prototypes:
O
O int read (int Handle, void Buffer, unsigned Length);
O Example:
O
O read (Input, &C, 1);
O 7iting
O The C++ rite function is used to write data to a file for handle level access.
O The handle write function must be supplied with (as an arguments):
o The handle of the logical file
o The address of the buffer from which the data will be written
o The number of bytes to be write
O The value returned by the rite function is the number of bytes written.
O Prototypes:
O
O int write (int Handle, void Buffer, unsigned Length);
O Example:
O
O write (Jutput, &C, 1);
O $eeking
O The C++ lseek function is used to move the file pointer of a file identified by its handle.
O The handle lseek function must be supplied with (as an arguments):
o The handle of the logical file
o The reference point of the seek
o The offset from the designated reference point
O The value returned by the lseek function is the new offset of the file pointer from the
beginning of the file
O Prototypes:
O
O long lseek (int Handle, long Jffset, int Jrigin);
O Example:
O
O lseek (Jutput, 100, SEEK_BEG);
O The Origin argument should be one of the folloing, to designate the reference point:
SEEK_SET
Beginning of file
SEEK_CUR
Current file position
SEEK_END
End of file
O etecting End 41 Fie
O The C++ eof function is used to detect when the file pointer of an fstream is at end of
file..
O The handle eof function has one argument.
o The handle of the logical file
O The value returned by the eof function is 1 if end of file is true and 0 if end of file is false.
O Prototypes:
O
O int eof (int Stream);
O Example:
O
O if (eof (Input))
O cout << "End of File\n";
ExampIe
/
File: List1.cpp
Description: File list program
using handle I/J
/

#include <io.h
#include <fcntl.h

int main (int, char ,) ,
/ Local Declarations /
int Input;
char C;
char FileName31,;
unsigned int Cursor;

/ Initialization /
write (1, "File name. ", 12);
read (0, FileName, 31);
Cursor = 0;
while (Cursor < 31) ,
if (FileNameCursor, == '\n')
FileNameCursor, = 0;
++Cursor;
,
Input = open (FileName, J_RDJNLY);

/ Algorithm /
while (!eof(Input)) ,
read (Input, &C, 1);
write (1, &C, 1);
,

/ Finalization /
close (Input);

/ Report to system /
return 0;
,

Reference Links
N4 C++ open Function
N4 C++ close Function
N4 C++ read Function
N4 C++ write Function
N4 C++ lseek Function
N4 C++ eof Function
FiIe Access with FILE Structures
Remarks
O For FLE level access, the logical file is declared as a pointer to a FLE (FLE *)
O The FLE structure is defined in the stdio.h header file. item`
Opening
tem`* The C++ fopen function is used to open a file for FLE level access.
O The FLE fopen function must be supplied with (as arguments):
o The name of the physical file
o The access mode
O The value returned by the fopen is a pointer to an open FLE, and is assigned to the file
variable.
O Prototypes:
O
O FILE fopen (const char Filename, char Access);
O Example:
O
O FILE Input;
O Input = fopen ("Daily.txt", "r");
O The access mode should be one of the followint strings:
7
Open for reading (existing file only) in text mode
7-
Open for reading (existing file only) in binary mode
79
Open for reading (existing file only) in text mode
7
Open for update (existing file only)
7-
Open for update (existing file only) in binary mode
79
Open for update (existing file only) in text mode
w
Open (or create) for writing (and delete any previous data)
w-
Open (or create) for writing (and delete any previous data) in binary mode
w9
Open (or create) for writing (and delete any previous data) in text mode
w
Open (or create) for update (and delete any previous data)
w-
Open (or create) for update (and delete any previous data) in binary mode
w9
Open (or create) for update (and delete any previous data) in text mode
a
Open (or create) for append with file pointer at current EOF (and keep any previous
data) in text mode
a-
Open (or create) for append with file pointer at current EOF (and keep any previous
data) in binary mode
a9
Open (or create) for append (and keep any previous data) in text mode
a
Open (or create) for append update (and keep any previous data)
a-
Open (or create) for append update (and keep any previous data) in binary mode
a9
Open (or create) for append update (and keep any previous data) in text mode
O 48ing
O The C++ f.lose function is used to close a file for FLE level access.
O The FLE fclose function must be supplied with (as an argument):
o A pointer to the FLE structure of the logical file
O The value returned by the f.lose is 0 if the close succeeds, and &neq;0 if the close fails..
O Prototypes:
O
O int fclose (FILE Stream);
O Example:
O
O fclose (Input);
O #eading
O The C++ fre,d function is used to read data from a file for FLE level access.
O The FLE fread function must be supplied with (as an arguments):
o A pointer to the FLE structure of the logical file
o The address of the buffer into which the data will be read
o The number of items to be read
o The size of each item to be read, in bytes
O The value returned by the fre,d function is the number of items read.
O Prototypes:
O
O size_t fread (void Buffer, size_t Size, size_t Count, FILE
Stream);
O Example:
O
O fread (&C, 1, 1, Input);
O 7iting
O The C++ frite function is used to write data to a file for FLE level access.
O The FLE fwrite function must be supplied with (as an arguments):
o A pointer to the FLE structure of the logical file
o The address of the buffer from which the data will be written
o The number of items to be written
o The size of each item to be written, in bytes
O The value returned by the frite function is the number of items written.
O Prototypes:
O
O size_t fwrite (void Buffer, size_t Size, size_t Count, FILE
Stream);
O Example:
O
O fwrite (&C, 1, 1, Jutput);
O $eeking
O The C++ fseek function is used to move the file pointer of a file identified by its FLE
structure.
O The FLE fseek function must be supplied with (as an arguments):
o A pointer to the FLE structure of the logical file
o The reference point of the seek
o The offset from the designated reference point
O The value returned by the fseek function is the new offset of the file pointer from the
beginning of the file
O Prototypes:
O
O long fseek (FILE Stream, long Jffset, int Jrigin);
O Example:
O
O fseek (Jutput, 100, SEEK_BEG);
O The Origin argument should be one of the folloing, to designate the reference point:
SEEK_SET
Beginning of file
SEEK_CUR
Current file position
SEEK_END
End of file
O etecting End 41 Fie
O The C++ feof function is used to detect when the file pointer of an fstream is past end of
file..
O The FLE feof function has one argument.
o A pointer to the FLE structure of the logical file
O The value returned by the feof function is 1 if end of file is true and 0 if end of file is false.
O Prototypes:
O
O int feof (FILE Stream);
O Example:
O
O if (feof (Input))
O cout << "End of File\n";
ExampIe
/
File: List2.cpp
Description: File list program
using FILE I/J
/

#include <stdio.h

int main (int, char ,) ,
/ Local Declarations /
FILE Input;
char C;
char FileName20,;

/ Initialization /
printf ("File name. ");
scanf ("%s", FileName);
Input = fopen (FileName, "r");

/ Algorithm /
fread (&C, 1, 1, Input);
while (!feof(Input)) ,
fwrite (&C, 1, 1, stdout);
fread (&C, 1, 1, Input);
,

/ Finalization /
fclose (Input);

/ Report to system /
return 0;
,
Reference Links
N4 C++ fopen Function
N4 C++ fclose Function
N4 C++ fread Function
N4 C++ fwrite Function
N4 C++ fseek Function
N4 C++ feof Function
FiIe Access with fstream objects
Remarks
O For fstream level access, the logical fstream is declared as an fstream.
O The fstream class is defined in the fstream.h header file.
O Opening
O The C++ open method is used to open an fstream for fstream level access.
O The fstream open method must be supplied with (as arguments):
o The name of the physical fstream
o The access mode
o Optionally, the access mode
O No value is returned by the open method.
O Prototypes:
O
O void fstream::open (const char FileName, int Access,
O int Protection = filebuf::openprot)
O Example:
O
O fstream Input;
O Input.open ("Daily.txt", ios::in);
O The access mode should be the bitwise OR of one or more of the following:
ios::in
Read access
ios::ou9
Write access
ios::a90
Seek to eof upon original open
ios::app
Append mode: all additions at eof
ios::97unc
Truncate file if already exists
ios::noc70a90
Open fails if file doesn't exist
ios::no70plac0
Open fails if file already exists
O 48ing
O The C++ .lose method is used to close an fstream for fstream level access.
O The fstream close method has no argument:
O No value is returned by the .lose method.
O Prototypes:
O
O void fstream :: close ();
O Example:
O
O Input.close ();
O #eading
O The C++ re,d method is used to read data from an fstream for fstream level access.
O The fstream read method must be supplied with (as an arguments):
o The address of the buffer to which the data will be read
o The number of bytes to be read
O The value returned by the re,d method is a reference to the fstream.
O Prototypes:
O
O fstream & fstream::read (char String, int Size);
O Example:
O
O Input.read (&C, 1);
O 7iting
O The C++ rite method is used to write data to an fstream for fstream level access.
O The fstream write method must be supplied with (as an arguments):
o The address of the buffer from which the data will be written
o The number of bytes to be written
O The value returned by the rite method is a reference to the fstream.
O Prototypes:
O
O fstream & fstream::write (char String, int Size);
O Example:
O
O Input.write (&C, 1);
O $eeking
O The C++ seek and seekp methods are used to move the fstream pointer of an fstream.
O The seekg method moves the read pointer.
O The seekp method moves the write pointer.
O n most cases, the read pointer and the write pointer are the same pointer.
O The fstream seek and seekp methods must be supplied with (as arguments):
o The reference point of the seek
o The offset from the designated reference point
O The value returned by the seek and seekp methods is a reference to the fstream
object.
O Prototypes:
O
O fstream & fstream::seekg (streamoff Jffset,
O ios::seek_dir Jrigin);
O fstream & fstream::seekp (streamoff Jffset,
O ios::seek_dir Jrigin);
O Example:
O
O Input.seekg (200, ios::beg);
O The Origin argument should be one of the folloing, to designate the reference point:
ios::beg
Beginning of file
ios::cur
Current file position
ios::end
End of file
O etecting End 41 Fie
O The C++ eof method is used to detect when the file pointer of an fstream is past end of
file..
O The fstream eof method has no arguments.
O The value returned by the eof method is 1 if end of file is true and 0 if end of file is false.
O Prototypes:
O
O int fstream::eof ();
O Example:
O
O if (Input.eof ())
O cout << "End of File\n";
ExampIe
/
File: List4.cpp
Description: File list program
using stream I/J
/

#include <iostream.h
#include <fstream.h

int main (int, char ,) ,
/ Local Declarations /
fstream Input;
char C;
char FileName20,;

/ Initialization /
cout << "File name. ";
cin FileName;
Input.open (FileName, ios::in);
Input.unsetf (ios::skipws);

/ Algorithm /
Input.read (&C, 1);
while (!Input.eof()) ,
cout << C;
Input.read (&C, 1);
,

/ Finalization /
Input.close ();

/ Report to system /
return 0;
,
Reference Links
N4 C++ fstream::open Method
N4 C++ fstream::close Method
N4 C++ fstream::read Method
N4 C++ fstream::write Method
N4 C++ fstream::seekg Method
N4 C++ fstream::seekp Method
N4 C++ fstream::eof Method
FiIe-ReIated Header FiIes
Remarks
O Header files can vary with the C++ implementation.
Reference Links
N4 io.h handle level /O
N4 fcntl.h handle level /O
N4 stdio.h FLE stream level /O
N4 iostream.h class iostream level /O
N4 fstream.h class iostream level /O


The Unix Directory Structure
Remarks
O n UNX, the directory structure is a single tree for the entire file system.

O n UNX, separate disks appear as subdirectories of the root (/).
O n UNX, the subdirectories of a pathname are separated by the forward
slash character (/).
O Example: /usr/bin/perl
O The directory structure of UNX is actually a graph, since symbolic links
allow entries to appear at more than one location in the directory structure.
The DOS Directory Structure
Remarks
O n DOS, the directory structure is a forest.

O n DOS, each disk volume has a separate directory tree.
O n DOS, the subdirectories of a pathname are separated by the backward
slash character (\).
O n DOS, the volume is designated in the pathname by a letter followed by a
semicolon (:).
O n DOS, each disk volume is identified by a letter.
O Example: C:\programs\dev\perl.exe
The Windows Directory Structure
Remarks
O Windows adds a "My Computer" node to to combine the drives of the DOS
forest, and a "desktop" node as a root node:

O The term "folder" is Windows Newspeak for "directory."
PhysicaI Devices and LogicaI FiIes
Terms
standard I/O
Logical files opened by the operating system and supplied to executing
processes.
I/O redirection
The redefinition of standard input or output to specified files or devices.
pipe
A connection between standard output of one process and standard input of
a second process.
Remarks
O Both DOS and UNX treat physical devices in the file system.
O This allows devices to be handled in the same way as disk files by
programs.
O n UNX, devices appear as nodes in the directory tree.
O n DOS, devices are give keywords as "file" names.
o CON = the console
o PRN = the printer
o COM1 = the first serial port
o NUL = a null "file" - the "bit bucket"
O Standard files are opened by the operating system before running a
program:
HandIe FILE iostream Description
0 stdin cin Standard nput
1 stdout cout Standard Output
2 stderr cerr Standard Error
O n both DOS and UNX, the standard output of a program can be redirected
to a file with the symbol.
O n both DOS and UNX, the standard input of a program can be redirected to
a file with the symbol.
O n both DOS and UNX, the standard output of one program can be piped
(connected) to the standard input of another program with the symbol.
Unix FiIe System Commands
Remarks
O
DOS UNIX Description
type filen,2e cat filen,2e Type the contents of a file
tail filen,2e Type the last lines of a file
copy filen,2e filen,2e cp filen,2e filen,2e Copy a file
move filen,2e filen,2e mv filen,2e filen,2e Move a file
del filen,2e rm filen,2e Delete a file
attrib fl,s filen,2e chmod 2ode filen,2e Change the protection mode
dir ls List contents of a directory
md
mkdir
mkdir Create directory
rd
rmdir
rmdir Remove directory


hLLp//wwwcomsclus/fs/noLes/lndexhLml

You might also like