File Organization-Lec2
File Organization-Lec2
2
Where do File Structures Fit in Computer Science?
➢ Any modern computer system will incorporate (at least) two
levels of storage:
• Primary storage : random access memory (RAM):
o Typical capacity 32MB to 1GB.
o Cost per MB $3.00.
o Typical access time 5ns to 60ns.
• Secondary storage: magnetic disk/ optical devices/ tape systems:
o Typical capacity 4GB to 100GB for fixed media; ¥ for removable.
o Cost per MB $0.01 for fixed media, more for removable.
o Typical access time 8ms to 12ms for fixed media, larger for
removable.
3
Physical Files and Logical Files
➢ Physical file: a collection of bytes stored on a disk or tape.
➢ Logical file: an “interface" that allows the application programs to access the
physical file on the secondary storage.
• A “Channel” (like a telephone line) that hides the details of the file’s location and
physical format to the program.
➢ The operating system is responsible for associating a logical file in a program to a
physical file in a secondary storage. Writing to or reading from a file in a program is
done through the operating system.
4
Physical Files and Logical Files
5
File Attributes
➢ Name – only information kept in human-readable form.
➢ Identifier – unique tag (number) identifies file within file system.
➢ Type – needed for systems that support different types.
➢ Location – pointer to file location on device.
➢ Size – current file size.
➢ Protection – controls who can do reading, writing, executing.
➢ Time, date, and user identification – data for protection, security, and usage
monitoring.
➢ Information about files are kept in the directory structure, which is maintained on the
disk.
6
Files and Directories
➢ There are two basic things that are stored on disk as part of the area controlled
by the file system:
• Files (store content)
• Directory information:
• Keeps info about files, their attributes or locations; help user to organize files.
Disk
Directory attrs file
attrs file
filename
filename
filename attrs File (content)
7
File Operations
• Common Operations that are supported by the C++ programming:
• Opening
• Closing
• Reading
• Writing
• Seeking
8
Files
• The physical file has a name, for instance myfile.txt
• The logical file has a logical name (a variable) inside the program.
• In C :
• FILE * outfile;
• In C++:
• fstream outfile;
• In Python:
• outfile = open("myfile.txt", "x")
• In Java:
• import java.io.File; // Import the File class
• File outfile = new File("myfile.txt ");
9
Opening Files
• Opening Files:
• links a logical file to a physical file.
• Once we have a logical file identifier hooked up to a physical file or device, we need
to declare what we intend to do with the file:
• Open an existing file
• Create a new file
• We are positioned at the beginning of the file and are ready to read or write.
10
Opening Files in C, C++ and Python
• In C:
FILE * outfile;
outfile = fopen(“myfile.txt”, “w”);
• In C++:
fstream outfile;
outfile.open(“myfile.txt”, ios::out);
• In Python:
outfile = open("myfile.txt", "x")
11
Opening Files in C and C++
#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;
}
12
Opening Files in C and C++
#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;
while (! infile.fail()) {
cout << c;
infile >> c ; }
infile.close() ;
return 0;
}
13
Testing for Open Errors
dataFile.open(“cust.dat”, ios::in);
if (!dataFile)
{
cout << “Error opening file.\n”;
}
14
Another way to Test for Open Errors
dataFile.open(“cust.dat”, ios::in);
if (dataFile.fail())
{
cout << “Error opening file.\n”;
}
15
Closing Files
• Cuts the link between the physical and logical files.
• Makes the logical file name available for another physical file (it’s like hanging up
the telephone after a call).
• Ensures that everything has been written to the file [since data is written to a buffer
prior to the file].
• Files are usually closed automatically by the operating system (unless the program is
abnormally interrupted).
16
Closing Files
• After closing a file, the logical name is free to be associated to another physical file.
• Closing a file used for output guarantees everything has been written to the physical
file. (When the file is closed the leftover from the buffers in the main memory is
flushed to the file on the secondary storage.)
• In C :
fclose(outfile);
• In C++ :
outfile.close();
• In Python:
outfile.close()
17
Closing Example
// This program demonstrates the close function.
#include <iostream.h>
#include <fstream.h>
void main(void) {
fstream dataFile;
dataFile.open("testfile.txt", ios::out);
if (!dataFile) {
cout << "File open error!" << endl;
return;
}
cout << "File was created successfully.\n";
cout << "Now closing the file.\n";
dataFile.close();
}
18
Reading
• Read data from a file and place it in a variable inside the program.
• Source_file = location the program reads from, i.e., its logical file name
• Destination_addr = first address of the memory block where we want to store the data.
• Size = how much information is being brought in from the file (byte count).
19
Reading
• In C:
char c;
FILE * infile;
infile = fopen(“myfile.txt”,”r”);
fread(&c, 1, 1, infile);
• . In C++:
char c;
fstream infile;
infile.open(“myfile.txt”,ios::in);
infile >> c;
• In Python:
outfile = open("myfile.txt", ”r”)
print(outfile.read())
20
Reading Example
// This program uses the >> operator to read information from a file.
#include <iostream.h>
#include <fstream.h>
void main(void) {
fstream dataFile;
char name[81];
dataFile.open("demofile.txt", ios::in);
if (!dataFile) {
cout << "File open error!" << endl;
return; }
cout << "File opened successfully.\n";
cout << "Now reading information from the file.\n\n";
for (int count = 0; count < 4; count++) {
dataFile >> name;
cout << name << endl; }
dataFile.close();
cout << "\nDone.\n";
}
21
Detecting the End of a File
• The eof() member function reports when the end of a file has been encountered.
if (inFile.eof())
inFile.close();
• The eof() function returns true when there is no more information to be read.
22
eof() Example
// This program uses the file stream object's eof() member
// function to detect the end of the file.
#include <iostream.h>
#include <fstream.h>
void main(void) {
fstream dataFile;
char name[81];
dataFile.open("demofile.txt", ios::in);
if (!dataFile) {
cout << "File open error!" << endl;
return;
}
cout << "File opened successfully.\n";
cout << "Now reading information from the file.\n\n"; dataFile >> name;
// Read first name from the file
23
eof() Example
while (!dataFile.eof()) {
cout << name << endl;
dataFile >> name;
}
dataFile.close();
cout << "\nDone.\n";
}
24
Writing
• Write data from a variable inside the program into the file.
• Destination_file = the logical file name where the data will be written.
• Source_addr = first address of the memory block where the data to be written is stored.
• Size = the number of bytes to be written.
25
Writing
• In C:
char c;
FILE * outfile;
outfile = fopen(“mynew.txt”,”w”);
fwrite(&c, 1, 1, outfile);
• In C++:
char c;
fstream outfile;
outfile.open(“mynew.txt”,ios::out);
outfile << c;
In Python:
f = open("demofile3.txt", "w")
f.write(“CSW241 File Organization")
26
Writing Example
// This program uses the << operator to write information to a file.
#include <iostream.h>
#include <fstream.h>
void main(void) {
fstream dataFile;
char line[81];
dataFile.open("demofile.txt", ios::out);
if (!dataFile) {
cout << "File open error!" << endl;
return;
}
cout << "File opened successfully.\n";
cout << "Now writing information to the file.\n";
dataFile << "Jones\n";
dataFile << "Smith\n";
dataFile << "Willis\n";
dataFile << "Davis\n";
dataFile.close();
cout << "Done.\n"; }
27
Another Writing Example
// This program writes information to a file, closes the file,
// then reopens it and appends more information.
#include <iostream.h>
#include <fstream.h>
void main(void) {
fstream dataFile;
dataFile.open("demofile.txt", ios::out);
dataFile << "Jones\n";
dataFile << "Smith\n";
dataFile.close();
dataFile.open("demofile.txt", ios::app);
dataFile << "Willis\n";
dataFile << "Davis\n";
dataFile.close();
}
28
Binary Files
• Binary files contain data that is unformatted, and not necessarily stored as ASCII
text.
29
Example
// This program uses the write and read functions.
#include <iostream.h>
#include <fstream.h>
void main(void)
{
fstream file(“NUMS.DAT", ios::out | ios::binary);
int buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "Now writing the data to the file.\n";
file.write((char*)buffer, sizeof(buffer));
file.close();
file.open("NUMS.DAT", ios::in); // Reopen the file.
cout << "Now reading the data back into memory.\n";
file.read((char*)buffer, sizeof(buffer));
for (int count = 0; count < 10; count++)
cout << buffer[count] << " ";
file.close();
}
30
Another Example
struct Person {
char name[50];
int age;
char phone[24];
};
int main() {
Person me = {“Mohammed", 28, "364-2534"};
Person book[30];
int x = 123;
double fx = 34.54;
ofstream outfile;
outfile.open("myData.dat", ios::binary | ios::out);
outfile.write(&x, sizeof(int)); // sizeof can take a type
outfile.write(&fx, sizeof(fx)); // or it can take a variable name
outfile.write(&me, sizeof(me));
outfile.write(book, 30*sizeof(Person))
outfile.close();
}
31
Another Example
int main()
{
int x;
ifstream infile;
infile.open("silly.dat", ios::binary | ios::in)
infile.read(&x, 7); // reads 7 bytes into a cell that is either 2 or 4
}
32
Seeking
• Used for direct access; an item can be accessed by specifying its position in the file.
• A program does not necessarily have to read through a file sequentially: It can jump to
specific locations in the file or to the end of file so as to append to it.
• The action of moving directly to a certain position in a file is often called seeking.
• Seek(Source_file, Offset)
• Source_file = the logical file name in which the seek will occur
• Offset = the number of positions in the file the pointer is to be moved from the start of
the file.
33
Seeking
• In C:
fseek(infile,0, 0); // moves to the beginning
fseek(infile, 0, 2); // moves to the end
fseek(infile,-10, 1); //moves 10 bytes from
//current position
• . In C++:
infile.seekg(0,ios::beg);
infile.seekg(0,ios::end);
infile.seekg(-10,ios::cur);
34
The tellp and tellg Member Functions
• tellp returns a long integer that is the current byte number of the file’s write position.
• tellg returns a long integer that is the current byte number of the file’s read position.
35
The tellp and tellg Member Functions
// This program demonstrates the tellg function.
#include <iostream.h>
#include <fstream.h>
#include <ctype.h> // For toupper() function
void main(void) {
fstream file("letters.txt", ios::in);
long offset;
char ch, again;
do {
cout << "Currently at position " << file.tellg() << endl;
cout << "Enter an offset from the beginning of the file: ";
cin >> offset;
36
The tellp and tellg Member Functions
file.seekg(offset, ios::beg);
file.get(ch);
cout << "Character read: " << ch << endl;
cout << "Do it again? ";
cin >> again;
} while (toupper(again) == 'Y');
file.close();
}
37
Example
// This program demonstrates the seekg function.
#include <iostream.h>
#include <fstream.h>
void main(void)
int main() {
int x;
streampos pos;
ifstream infile;
infile.open(“myData.dat", ios::binary | ios::in);
infile.seekp(243, ios::beg); // move 243 bytes into the file
infile.read(&x, sizeof(x));
pos = infile.tellg();
cout << "The file pointer is now at location " << pos << endl;
infile.seekp(0,ios::end); // seek to the end of the file
infile.seekp(-10, ios::cur); // back up 10 bytes
infile.close();
}
38
Another Example
// This program demonstrates the seekg function.
#include <iostream.h>
#include <fstream.h>
void main(void)
{
fstream file("letters.txt", ios::in);
char ch;
file.seekg(5L, ios::beg);
file.get(ch);
cout << "Byte 5 from beginning: " << ch << endl;
file.seekg(-10L, ios::end);
file.get(ch);
cout << "Byte 10 from end: " << ch << endl;
file.seekg(3L, ios::cur);
file.get(ch);
cout << "Byte 3 from current: " << ch << endl;
file.close();
}
39