File Structures (2) Konkuk University (DB Lab.) 2
Chapter Objectives
Describe the process of linking a logical file within a program to an actual physical file of deviceDescribe the procedures used to create, open and close filesIntroduce the C++ input and output classesExplain the use of overloading in C++Describe the procedures used for reading from and writing to filesIntroduce the concept of position within a file and describe procedures for seeking different positionsProvide an introduction to the organization of hierarchical file systemsPresent the Unix view of a file and describe Unix file operations and commands based on this view
File Structures (2) Konkuk University (DB Lab.) 3
Chapter Outline
2.1 Physical Files and Logical Files
2.2 Opening Files
2.2 Closing Files
2.4 Reading and Writing
2.5 Seeking
2.6 Special Characters in Files
2.7 The Unix Directory Structures
2.8 Physical Devices and Logical Files
2.9 File-Related Header Files
2.10 Unix File System Commands
File Structures (2) Konkuk University (DB Lab.) 4
2.1 Physical Files and Logical Files
Filea particular collection of bytes
Physical filea file on a disk or tape
Logical filea file used inside the program
(ex) select inp_file assign to “myfile.dat”. : Cobolassign (inp_file, 'myfile.dat') : Turbo Pascal
logical file physical file
File Structures (2) Konkuk University (DB Lab.) 5
2.1 Physical Files and Logical Files
File Structures (2) Konkuk University (DB Lab.) 6
2.2 Opening Files
Two options(1) open an existing file
position at the beginning of the file and ready to start readingand writing
(2) create a new fileready for use after creation
C++ and C (fcntl.h)fd = open(filename, flags [, pmode]); => pp.17-18
(ex) fd = open(filename, O_RDWR | O_CREAT, 0751);fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0751);fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0751);
File Structures (2) Konkuk University (DB Lab.) 7
2.3 Closing Files
Closing a filethe logical name or file descriptor is available for use with another file (i.e., breaks the link)ensure that everything has been written to the file
(i.e., the buffer for the file has been flushed of data and everything we have written has been sent to the file)automatically closed by OS when a program terminates normally
(=> for protection against data loss and for reuse of logicalfilenames)
File Structures (2) Konkuk University (DB Lab.) 8
2.4 Reading and Writing (1/4)
Read and Write FunctionsBefore reading or writing, we must have already opened the file.
low level read or writeREAD (Source_file, Destination_addr, Size)
logical file first addr. of the byte count name memory block
WRITE(Destination_file, Source_addr, Size)
File Structures (2) Konkuk University (DB Lab.) 9
2.4 Reading and Writing (2/4)
Files with-C Streams & C++ Stream Classes stream : a file or some other source or consumer of file
(1) C Streams or C input/outputuse the standard C functions in stdio.hstdio.h contains definitions of the types & the operations on C streamsstdio & stdout : standard input and output streamsfile = fopen(filename, type);=> pp.21-22fread, fget, fwrite, fput, fscanf, fprintf
(2) C++ stream classesuse the stream classes of iostream.h and fstream.hcin, cout : predefined stream objects for the standard input & standard output filesfstream : class for access to files has two constructors and methods, open, read, write>>(extraction) and <<(insertion) : overloaded for input and output
File Structures (2) Konkuk University (DB Lab.) 10
2.4 Reading and Writing (3/4)
Programs in C++ to Display the Contents of a File1. Display a prompt for the name of the input file2. Read the user’s response from the keyboard into a variable
called filename3. Open the file for input4. While there are still characters to be read from the input file
1. Read a character from the file2. Write the character to the terminal screen
5. Close the input file
Ex)Figure 2.2 : using C streamsFigure 2.3 : using C++ stream classes=> See Appendix D
File Structures (2) Konkuk University (DB Lab.) 11
2.4 Reading and Writing (4/4)
Detecting End-of-fileC
fread call returns the 0 of elements read
C++use the function fail to check end-of-file
File Structures (2) Konkuk University (DB Lab.) 12
2.5 Seeking (1/3)
Seekingto control the movement of the read/write pointerSeek (Source_file, Offset)
Source_file : logical file nameOffset : the # of positions from the start of the file
(ex) Seek(data, 373)move directly from the origin to the 373 position
File Structures (2) Konkuk University (DB Lab.) 13
2.5 Seeking (2/3)
Seeking with C Streams pos = fseek(fd, byte-offset, origin)
long integerto set the read/write pointer to any byte in a file
(ex) pos = fseek (fd, 373, 0);
0 : 시작1 : 현위치2 : 마지막
File Structures (2) Konkuk University (DB Lab.) 14
2.5 Seeking (3/3)
Seeking with C++ Stream Classesalmost exactly the same as in C streamsTwo syntactic differences
(1) an object of fstream has two file pointers, get pointer and putpointer
=>seekg for the get pointer and seekp for the put pointer(2) seek operations are methods of the stream classes=>file.seekg(byte_offset, origin)
file.seekp(byte_offset, origin)where origin = ios::beg, ios::cur, and ios::end
(ex) file.seekg(373, ios::beg);file.seekp(373, ios::beg);
File Structures (2) Konkuk University (DB Lab.) 15
2.7 The UNIX Directory Structure (1/2)
UNIX file system a tree-structured organization with two kinds of files ( i.e., regular files(programs and data) and directories)
devices such as tape or disk drivers are also files (in devdirectory)
“/”to indicate the root directory
to separate directory names from the file name
absolute pathname and relative pathname for file identificationcurrent directory : .
parent directory : ..
File Structures (2) Konkuk University (DB Lab.) 16
2.7 The UNIX Directory Structure (2/2)
/ (root)
bin usr usr6 dev
adb cc yaccbin lib lib mydir
console kbd TAPE
libc.a libm.a
libdf.aaddr DF
File Structures (2) Konkuk University (DB Lab.) 17
2.8 Physical Devices and Logical Files
Physical Devices as Filesfile in UNIX
a sequence of bytes ( => very few operations )magnetic disk and devices like the keyboard and the console are also files (/dev/kbd, /dev/console)represented logically by an integer (file descriptor)
File Structures (2) Konkuk University (DB Lab.) 18
2.8 Physical Devices and Logical Files
The Console, the Keyboard, and Standard Errordefined in stdio.h
Stdin(standard input) : keyboard Stdout(standard output): console Stderr(standard error) : console
Read and writeread ... gets <--- stdinwrite ... printf ---> stdout
File Structures (2) Konkuk University (DB Lab.) 19
2.8 Physical Devices and Logical Files
I/O Redirection and Pipesfor switching between standard I/O (stdin and stdout) and regular file I/OI/O redirection
to specify at execution time alternate files for input or output< file ( redirect stdin to "file" )> file ( redirect stdout to "file" )(ex) list > myfile
Pipeto use the output of a program as input to another program without using an intermediate fileprogram1 | program2
any stdout output of program1 => any stdin input to program2
(ex) list | sort
File Structures (2) Konkuk University (DB Lab.) 20
2.9 File-Related Header Files
Header files ( /usr/include )
have special names and values
C streams : stdio.h
C++ streams : iostream.h and fstream.h
Unix operations : fcntl.h and file.h
EOF, stdin, stdout, stderr : stdio.h
O_RDONLY, O_WRONLY, O_RDWR : file.h
File Structures (2) Konkuk University (DB Lab.) 21
2.10 Unix File System Commands
Unix Commands
cat filenames tail filenames
cp file1 file2 mv file1 file2
rm filenames chmod mode filename
ls mkdir name
rmdir name
=> Consult a Unix manual for more information
File Structures (2) Konkuk University (DB Lab.) 22
A.1 File I/O in Pascal (1/2)
included in language definitionprovide high-level access to reading/writingin C, a file is a sequence of bytes, butin Pascal, a file is a sequence of “records”
File Structures (2) Konkuk University (DB Lab.) 23
A.2 File I/O in Pascal (2/2)
File I/O functionsassign(input_file, ‘myfile.dat’);
// associate between a logical file and a physical filereset(input_file); // open existing filerewrite(input_file); // create new fileappend(input_file); // open to add data to existing fileread(input_file, var); // read from file to variablereadln(input_file, var); // read from file to variablewrite(input_file, var); // write from variable to filewriteln(input_file, var); // write from variable to fileclose(input_file); // close file
File Structures (2) Konkuk University (DB Lab.) 24
A.3 File I/O in C
Low-level I/OUNIX system calls
fd1 = open(filename1, rwmode);fd2 = open(filename2, rwmode);read(fd1, buf, n);write(fd2, buf, n);lseek(fd1, offset, origin);close(fd1);close(fd2);
File Structures (2) Konkuk University (DB Lab.) 25
A.4 <stdio.h>fp = fopen(s, mode)
/* open file s; mode “r”, “w”, “a” for read, write, append (returns NULL for error) */c = getc(fp) /* get character; getchar() is getc(stdin) */putc(c, fp) /* put character; putchar(c) is putc(c, stdout) */ungetc(c, fp)
/* put character back on input file fp; at most 1 char can be pushed back at one time */scanf(fmt, a1, ....)
/* read characters from stdin into a1, ... according to fmt. Each ai must be a pointer. Returns EOF or number of fields converted */fscanf(fp, .....) /* read from file fp */printf(fmt, a1, ....) /* format a1, ... according to fmt, print on stdout */fprintf(fp, ....) /* print .... on file fp */fgets(s, n, fp)
/* read at most n characters into s from fp. Returns NULL at end of file */ fputs(s, fp) /* print string s on file fp */fflush(fp) /* flush any buffered output on file fp */ fclose(fp) /* close file fp */
File Structures (2) Konkuk University (DB Lab.) 26
A.5 File I/O in C++
#include <fstream.h>
File Stream: fstream, ifstream, ofstream(ex) ifstream f1(“input.fil”);
ofstream f2(“output.fil”, ios::out|ios::nocreat);
fstream f3(“inout.fil”, ios::in|ios::out);
f1.get(ch);
f1.eof();
f2.put(ch);
f2.bad();
f1.seekg(); f2.seekp();
f3.close();
File Structures (2) Konkuk University (DB Lab.) 27
A.6 <iostream.h> (1/3)
class ios
class istream: virtual public ios class ostream: virtual public ios
class iostream: public istream, public ostream
Class Hierarchy
File Structures (2) Konkuk University (DB Lab.) 28
class ostream: virtual public ios{ public:
ostream& put(char);ostream& write(char*, int); ostream& seekp(int);ostream& operator<<(char);ostream& operator<<(int);ostream& operator<<(char*);ostream& operator<<(long);ostream& operator<<(short);ostream& operator<<(float);..............
};
A.7 <iostream.h> (2/3)
class istream: virtual public ios{ public:
istream& get(char*, int, char = ‘₩n’);istream& get(char);istream& read(char*, int);istream& gets(char**, char = ‘₩n’);istream& seekg(int);istream& operator>>(char&);istream& operator>>(int&);istream& operator>>(char*);istream& operator>>(long&);...............
};
File Structures (2) Konkuk University (DB Lab.) 29
A.8 <iostream.h> (3/3)
class iostream: public istream, public ostream { public: iostream( ) { } };
File Structures (2) Konkuk University (DB Lab.) 30
A.9 Copy Program in C++ (1/2)
#include <fstream.h>#include <libc.h>
void error(char *s, char *s2 = ““){cerr << s << ‘ ‘ << s2 << ‘₩n’; exit(1);}
int main(int argc, char *argv[]){
if( argc != 3) error(“wrong number of arguments”);
ifstream src(argv[1]); //input file streamif (!src) error(“cannot open input file”, argv[1]);
File Structures (2) Konkuk University (DB Lab.) 31
A.9 Copy Program in C++ (2/2)
ofstream dest(argv[2]); //output file streamif(!dest) error(“cannot open output file”, argv[2]);
char ch;while( src.get(ch) ) dest.put(ch);
if(!src.eof() || dest.bad()) error(“something strange happened !”);
return 0;}