Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
© File in Unix
◆a particular collection of bytes or sequence of bytes
© Physical file
◆a file on a disk or tape
© Logical file
◆a file used inside the program
Logical File: A “Channel” (like a telephone line) that hides the details of
the file’s location and physical format to the program.
When a program wants to use a particular file, “data”, the operating
system must find the physical file called “data” and make the hookup
by assigning a logical file to it.
This logical file has a logical name which is what is used inside the
program.
2.1 Physical Files and Logical Files
Dr K.Srinivas
Adv Data Structure
5
2.2 Opening Files
Two options
(1)open an existing file
position at the beginning of the file and ready to start reading
and writing
(2)create a new file
ready for use after creation
We can open an existing file or create a new one in C
through the
UNIX system function open( ) .
This function takes two required arguments and a third
argument
that is optional:
fdDr K.=Srinivas
open(filename, flags [, pmode]); 6
2.2 Opening Files
Dr K.Srinivas
Big Data Analytics 7
2.2 Opening Files
Dr K. Srinivas 9
2.4 Reading and Writing (1/4)
Read and Write Functions
Before reading or writing, we must have already opened
the file.
Low level read or write:
Read (Source_file, Destination_addr, Size)
Dr K.Srinivas
Adv data structures 10
2.4 Files with-C Streams & C++ Stream
Classes(2/4)
Dr K. Srinivas 11
2.4 Programs in C++ to Display the
Contents of a File(3/4)
This first simple file processing program, which we call LIST, opens a file
for input and reads it, character by character, sending each character to
the screen after it is read from the file.
Dr K. Srinivas 12
2.4 Programs in C++ to Display
the Contents of a File(3/4)
Dr K.Srinivas
Big Data Analytics 13
2.4 Detecting End-of-file(4/4)
© Detecting End-of-file
◆ C
The read( ) call returns the number of bytes read.
If read( ) returns a value of zero, then the program
has reached the end of the file.
So, rather than using an eof( ) function, we construct
the while loop to run as long as the read() call finds
something to read.
fread call returns the 0 of elements read
◆ C++
use the function fail to check end-of-file
Dr K. Srinivas 14
2.5 Seeking
Read the file sequentially, reading one byte after another until we reach the end of
the file.
Every time a byte is read, the operating system moves the read/write pointer
ahead, and we are ready to read the next byte
Sometimes we want to read or write without taking the time to go through every
byte sequentially.
Perhaps we know that the next piece of information we need is 10,000 bytes away,
and so we want to jump there to begin reading.
Or perhaps we need to jump to the end of the file so we can add new information
there.
To satisfy these needs we must be able to control the movement of the read/write
pointer Dr K.Srinivas
Adv data structures 15
2.5 Seeking (1/3)
The action of moving directly to a certain position in a file is often called seeking.
A seek requires at least two pieces of information, expressed here as arguments
to the generic pseudocode function SEEK( ) :
© Seeking
Seek (Source_file, Offset)
Source_file : logical file name
Offset : the # of positions from the start of the file
◆ (ex) Seek(data, 373)
move directly from the origin to the 373 position
Dr K. Srinivas 16
2.5 Seeking with C Streams(2/3)
One of the features of UNIX that has been incorporated into C streams is the
ability to view a file as a potentially very large array of bytes that just happens to
be kept on secondary storage.
The C stream seek function, fseek, provides a similar capacity for files
Dr K. Srinivas 18
2.6 Special Characters in Files
Sometimes, the operting system attempts to make “regular”
user’s life easier by automatically adding or deleting
characters for them.
These modifications, however, make the life of programmers
building sophisticated file structures
Control-Z is added at the end of all files (MS-DOS). This is to
signal an end-of-file.
<Carriage-Return> + <Line-Feed> are added to the end of
each line (again, MS-DOS).
<Carriage-Return> is removed and replaced by a character
count on each line of text (VMS)
Dr K.Srinivas
Adv data structures 19
2.7 The Unix Directory Structure
In any computer systems, there are many files (100’s or
1000’s). These files need to be organized using some
method. In Unix, this is called the File System.
“.” stands for the current directory and “..” stands for the
parent directory.
Dr K.Srinivas
Adv data structures 21
2.7 The UNIX Directory Structure
© 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 dev directory)
◆“/”
◆ to indicate the root directory
to separate directory names from the file name
◆
◆absolute pathname and relative pathname for file identification
◆ current directory : .
◆ parent directory : ..
Dr K. Srinivas 22
2.7 The UNIX Directory Structure
/
(root)
bi us usr6 dev
n r
bi lib mydir
consol kbd
adb cc n li e TAP
yacc b
libdf. E
a add D
libc. F
r
a libm.a
Dr K. Srinivas 23
2.8 Physical Devices and Logical Files
Dr K. Srinivas 24
2.8 The Console, the Keyboard, and
Standard Error
Dr K. Srinivas 25
Stdout, Stdin, Stderr
Stdout --> Console
fwrite(&ch, 1, 1, stdout);
fread(&ch, 1, 1, stdin);
Dr K. Srinivas 27
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
Dr K. Srinivas 28
2.10 Unix File System Commands
cat filenames --> Print the content of the named textfiles.
tail filename --> Print the last 10 lines of the text file.
chmod mode filename --> Change the protection mode on the named file.
Dr K. Srinivas 30
A.2 File I/O in Pascal
(2/2)
© File I/O functions
◆ assign(input_file, ‘myfile.dat’);
// associate between a logical file and a physical file
◆ reset(input_file); // open existing file
◆ rewrite(input_file); // create new file
◆ append(input_file); // open to add data to existing file
◆ read(input_file, var); // read from file to variable
◆ readln(input_file, var); // read from file to variable
◆ write(input_file, var); // write from variable to file
◆ writeln(input_file, // write from variable to file
◆ var);
close(input_file); // close file
Dr K. Srinivas 31
A.3 File I/O
in C
© Low-level I/O
© UNIX 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);
Dr K. Srinivas 32
A.4 <stdio.h>
© fp = fopen(s, mode)
/* open file s; mode “r”, “w”, “a” for read, write, append (returns NULL for
© c = error) */ /* get character; getchar() is getc(stdin) */
© getc(fp) /* put character; putchar(c) is putc(c,
© ungetc(c,
putc(c, fp) stdout) */
fp) /* put character back on input file fp; at most 1 char can be pushed back at one
© scanf(fmt,
time */ 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 */
© fgets(s,
fprintf(fp,n, ....) /* print .... on file fp */
fp) /* read at most n characters into s from fp. Returns NULL at end of
© fputs(s,
file fp)
*/ /* print string s on file fp */
© fflush(fp) /* flush any buffered output on file
fp */
© fclose(fp) /* close file fp */
Dr K. Srinivas 33
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();
Dr K. Srinivas 34
A.6 <iostream.h> (1/3)
class
ios
Class Hierarchy
Dr K. Srinivas 35
A.7 <iostream.h> (2/3)
class ostream: virtual public ios class istream: virtual public ios
{ public: { public:
istream& get(char*, int, char = ‘₩n’);
ostream& put(char); ostream& istream& get(char); istream&
write(char*, int); ostream& read(char*, int);
seekp(int); ostream& istream& gets(char**, char = ‘₩n’);
operator<<(char); ostream& istream& seekg(int); istream&
operator<<(int); ostream& operator>>(char&); istream&
operator<<(char*); ostream& operator>>(int&); istream&
operator<<(long); ostream& operator>>(char*); istream&
operator<<(short); ostream& operator>>(long&);
operator<<(float); ...............
.............. }
}; ;
Dr K. Srinivas 36
A.8 <iostream.h>
(3/3)
class iostream: public
istream, public ostream
{
public:
iostream( ) { }
};
Dr K. Srinivas 37
A.9 Copy Program in C++
(1/2)
#include <fstream.h> #include <libc.h>
Dr K. Srinivas 38
A.9 Copy Program in C++
(2/2)
ofstream dest(argv[2]); //output file stream
if(!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;
}
Dr K. Srinivas 39