Lesson 2-3 Fundamental File Processing Operations
Lesson 2-3 Fundamental File Processing Operations
Content
Content
Sample programs for file manipulation
Physical files and logical files
Opening and closing files
Reading from files and writing into files
How these operations are done in C and C++
Standard input/output and redirection
File Organization
25
What
What is
is aa FILE?
FILE?
I wonder...
A file is...
A collection of data is placed under permanent or
non-volatile storage
Examples: anything that you can store in a disk,
hard drive, tape, optical media, and any other
medium which doesnt lose the information when
the power is turned off.
Notice that this is only an informal definition!
File Organization
26
Where
Where do
do File
File Structures
Structures fit
fit in
in CS?
CS?
Application
DBMS
File system
Operating System
Hardware
File Organization
27
Physical
Physical Files
Files &
& Logical
Logical Files
Files
Physical file: physically exists on secondary storage;
known by the operating system; appears in its file
directory
Logical file, what your program actually uses, a pipe
though which information can be extracted, or sent.
Operating system: get instruction from program or
command line; link logical file with physical file or device
Why is the distinction useful? Why not allow our
programs to deal directly with physical files?
File Organization
28
Basic
Basic File
File Operations
Operations
Opening a file - basically, links a logical file to a physical
file.
On open, the O/S performs a series operations that end
in the program that is trying to open the file being
assigned a file descriptor.
Additionally, the O/S will perform particular operations
on the file at the request of the calling program, these
operations are intended to initialize the file for use by
the program.
What happens when the O/S detects an error?
File Organization
29
hFile:
hFile: Logical
Logical File,
File, account.txt:
account.txt: Physical
Physical File
File
#include <stdio.h>
int main(){
FILE *hFile=fopen(account.txt,"r");
char c;
while (!feof(hFile)){
fread (&c,sizeof(char),1,hFile) ;
fwrite(&c,sizeof(char),1,stdout) ;
}
fclose(hFile) ;
return 0;
}
File Organization
30
FILE
FILE
typedef struct {
unsigned char *curp
curp;
unsigned char *buffer
buffer;
int
level;
int
bsize;
bsize
unsigned short istemp;
unsigned short flags;
wchar_t
hold;
char
fd;
unsigned char token;
}
FILE;
FILE
File Organization
C++
C++ Counterpart
Counterpart
#include <fstream>
#include <iostream>
using namespace std ;
int main(){
char c;
fstream infile ;
infile.open("account.txt",ios::in) ;
infile.unsetf(ios::skipws) ;
infile >> c ;
File Organization
32
while (! infile.fail()){
cout << c ;
infile >> c ;
}
infile.close() ;
return 0;
}
File Organization
33
Physical
Physical Files
Files &
& Logical
Logical Files
Files
Revisited
Revisited ## 11
OS is responsible for associating a logical file in a program to a
physical file in disk or tape. Writing to or reading from a file in a
program is done through the OS.
Note that from the program point of view, input devices (keyboard)
and output devices (console, printer, etc) are treated as files
places where bytes come from or sent to
There may be thousands of physical files on a disk, but a program
only have a limited number of logical files open at the same time.
The physical file has a name, for instance account.txt
The logical file has a logical name used for referring to the file
inside the program. The logical name is a variable inside the
program, for instance infile
File Organization
34
Physical
Physical Files
Files &
& Logical
Logical Files
Files
Revisited
Revisited ## 22
In C PL, this variable is declared as.
FILE *infile
*infile ;
In C++ PL, the logical name is the name of an object of
the class fstream:
fstream
fstream infile ;
In both languages, the logical name infile will be
associated to the physical file account.txt at the time of
opening the file.
File Organization
35
More
More on
on Opening
Opening Files
Files
Two options for opening a file:
Open an existing file
Create a new file
File Organization
36
How
How to
to do
do in
in C
C
FILE *outfile;
outfile = fopen(account.txt, w) ;
The 1st argument indicates the physical name of the file
The 2nd one determines the mode
the way the file is opened
File Organization
37
The
The Mode
Mode
r: open an existing file for reading
w: create a new file, or truncate existing one, for writing
a: open a new file, or append an existing one for writing
r+: open an existing file for reading and writing
w+: create a new file, or truncate an existing one for
reading and writing
a+: create a new file, or append an existing one for
reading and writing
File Organization
38
How
How to
to do
do in
in C++
C++
fstream outfile;
outfile.open(account.txt, ios::out) ;
The 1st argument indicates the physical name of the file
The 2nd argument is an integer indicating the mode
defined in the class ios.
ios
File Organization
39
The
The Mode
Mode
ios::in
open for reading
ios::out open for writing
ios::app seek to the end of file before each write
ios::trunc
always create a new file
ios::nocreate
fail if file does not exist
ios::binary
open in binary mode
File Organization
40
Basic
Basic File
File Operations
Operations
Closing a file - cuts the link between physical and logical
files
Upon closing, the OS takes care of synchronizing the
contents of the file, e.g. often a buffer is used, need to
write buffer content to file.
In general, files are automatically closed when the
program ends.
So, why do we need to worry about closing files?
In C: fclose(
fclose(outfile)
outfile)
In C++: outfile.
outfile.close()
close()
File Organization
41
Basic
Basic File
File Operations
Operations
Reading and Writing basic I/O operations.
Usually require three parameters: a logical file, an
address, and the amount of data that is to be read or
written.
What is the use of the address parameter?
File Organization
42
Reading
Reading in
in C
C
char c ; // a character
char a[100] ; // an array with 100 characters
FILE * infile ;
:
infile = fopen(myfile.txt, r) ;
fread(&c,1,1,infile) ; // reads one character
fread(a,1,10,infile) ; // reads 10 characters
File Organization
43
fread()
fread()
fread(&c,1,1,infile) ; // reads one character
fread(a,1,10,infile) ; // reads 10 characters
1st argument: destination address
2nd argument: element size in bytes
3rd argument: number of elements
4th argument: logical file name
File Organization
44
Reading
Reading in
in C++
C++
char c ; // a character
char a[100] ; // an array with 100 characters
fstream infile ;
infile.open(myfile.txt, ios::in) ;
infile >> c; // reads one character
infile.read(&c,1) ;
infile.read(a,10); // reads 10 bytes
Note that thanks to operator overloading in C++,
operator >> gets the same info at a higher level
File Organization
45
Writing
Writing in
in C
C
char c ; // a character
char a[100] ; // an array with 100 characters
FILE * outfile ;
outfile = fopen(myfile.txt, w) ;
fwrite(&c,1,1,outfile) ; // writes one character
fwrite(a,1,10,outfile) ; // writes 10 characters
File Organization
46
Writing
Writing in
in C++
C++
char c ; // a character
char a[100] ; // an array with 100 characters
fstream outfile ;
outfile.open(myfile.txt, ios::out) ;
outfile << c; // writes one character
outfile.write(&c,1) ;
outfile.write(a,10); // writes 10 bytes
File Organization
47
Additional
Additional File
File Operations
Operations
Seeking: source file, offset.
Detecting the end of a file
Detecting I/O error
File Organization
48
Seeking
Seeking in
in C
C
int fseek(FILE *stream, long offset, int whence);
Repositions a file pointer on a stream.
fseek sets the file pointer associated with stream to a new
position that is offset bytes from the file location given by
whence.
Whence must be one of the values 0. 1, or 2 which
represent three symbolic constants (defined in stdio.h) as
follows:
SEEK_SET
SEEK_CUR
SEEK_END
File Organization
0
1
2
File beginning
Current file pointer position
End-of-file
49
Seeking
Seeking with
with C++
C++ Stream
Stream Classes
Classes
A fstream has 2 file pointers: get pointer & put pointer
(for input)
(for output)
file1.seekg ( byte_offset, origin); //moves get pointer
file1.seekp ( byte_offset, origin); //moves put pointer
origin can be ios::beg
ios::cur
ios::end
(beginning of file)
(current position)
(end of file)
50
Calculating
Calculating File
File Size
Size
int main(int argc, char* argv[]) {
FILE *hFile=fopen(argv[1],"r");
fseek(hFile, 0L, SEEK_END);
int fileLength = ftell(hFile);
printf(\nFile size is %d,fileLength) ;
fclose(hFile) ;
return 0;
}
File Organization
51
Detecting
Detecting End
End of
of File
File
In C: Check whether fread returned value 0
int i ;
i = fread(&c,1,1,infile) ; //attempt to read
if (i==0) // true if file has ended
52
Logical
Logical File
File Names
Names Associated
Associated to
to Std
Std IO
IO Devices
Devices
Purpose
Default
Meaning
Logical Name
in C in C++
stdout cout
stdin
cin
stderr
cerr
File Organization
53
Redirection
Redirection
Some OS allow the default meanings to be changed
through a mechanism called redirection
Example in Unix
Suppose that prog is the executable program
Input redirection (standard input becomes file in.txt)
prog < in.txt
Output redirection (standard output becomes file out.txt)
prog > out.txt
You can also do
prog < in.txt > out.txt
File Organization
54