lecture3: File Organization
1
Seeking
• 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.
2
Sequential Search and Direct Access
Search for a record matching a given key.
• Sequential Search: Look at records sequentially until matching
record is found.
• Direct Access: Being able to seek directly to the beginning of the
record.
3
Simple file
Output to File test.txt
--------------------------------------------------------
Jones
Smith This is how the
Willis file will look like
Davis
4
Seeking
• The action of moving directly to a certain position in a file.
• Generic seek function :
Seek(Source_File, Offset)
Soururce_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.
• Example :
Seek(infile, 3030)
Moves to byte 3030 in file.
5
File seek operation
• A fstream has 2 file pointers:
• get pointer (for input)
• put pointer (for output)
• file1.seekg ( byte_offset, origin); //moves get pointer
• file1.seekp ( byte_offset, origin); //moves put pointer
origin can be
ios::beg (beginning of file)
ios::cur (current position)
ios::end (end of file)
6
File seek operation
Ex:
1- file1.seekg ( 373, ios::beg);
meaning moves get pointer 373 bytes from the beginning of file
2- file.seekp(20L, ios::beg);
20L : the L suffix forces the compiler to treat the number as a long integer.) This
statement moves the file’s write position to byte number 20. (All numbering starts
at 0, so byte number 20 is actually the twenty-first byte.)
7
File seek operation
Statement How it Affects the Read/Write Position
File.seekp(32, ios::beg); Sets the write position to the 33rd byte (byte 32) from the beginning of the file.
file.seekp(-10, ios::end); Sets the write position to the 11th byte (byte 10) from the end of the file.
file.seekp(120, ios::cur); Sets the write position to the 121st byte (byte 120) from the current position.
file.seekg(2, ios::beg); Sets the read position to the 3rd byte (byte 2) from the beginning of the file.
file.seekg(-100, ios::end); Sets the read position to the 101st byte (byte 100) from the end of the file.
file.seekg(40, ios::cur); Sets the read position to the 41st byte (byte 40) from the current position.
file.seekg(0, ios::end); Sets the read position to the end of the file.
8
Example
#include <iostream.h>
#include <fstream.h>
void main(void)
{ File contents:
fstream file(“d:\\letters.txt", ios::in);
char ch; abcdefghijk
f
file.seekg(5L, ios::beg);
file.get(ch);
cout << "Byte 5 from beginning: " << ch << endl;
file.seekg(-5L, ios::end); g
file.get(ch);
cout << "Byte 10 from end: " << ch << endl;
file.seekg(3L, ios::cur); k
file.get(ch);
cout << "Byte 3 from current: " << ch << endl;
file.close();
}
9
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.
10
Example 1
write a C++ program, with no loops, to copy the contents of the file
d:\\source.txt to the file d:\\destination.txt, assume that destination file is empty,
don’t use any loops.
int main ()
{
ifstream infile ("source.txt",ios::binary);
ofstream outfile ("destination.txt",ios::binary);
infile.seekg (0,infile.end);
long size = infile.tellg();
infile.seekg (0);//by default seeks from beginning
char* buffer = new char[size];
infile.read (buffer,size);
outfile.write (buffer,size);
delete[] buffer; outfile.close(); infile.close();
return 0;
}
Example 2
#include <fstream> //fstream, ifstream, ofstream
#include <string> //string, getline()
#include <iostream> //cout
using namespace std;
void simple_write_example()
{
ofstream file;
char line[] = "Text To File";
string str = "Hello";
file.open("d:\\simple.txt"); //default ==> ios::out | ios::trunc
file << line << endl;
file << 'A' << endl;
file << 1 << endl;
file << str << endl;
file.close();
}
void simple_read_example()
{
ifstream file; Output
char line[20]; char chr = ' '; -----------------------
int num = 0; string str = "";
file.open("d:\\simple.txt"); //default ios::in
line:Text To File
char:A
file.getline(line, 20); num:1
file >> chr; getline has an optional third str:Hello
file >> num; argument (character) that
file >> str; will end getline's input, the
default value is '\n'
cout << "line:" << line << endl;
cout << "chr:" << chr << endl;
cout << "num:" << num << endl;
cout << "str:" << str << endl;
file.close();
}
void simple_read_and_write_example()
{
fstream file;
string line;
file.open("d:\\simple.txt", ios::in | ios::out);
file << "This is a line 1" << endl;
file << "This is a line 2"<< endl;
file.seekg(0, ios::beg); Function
<string>
while (!file.eof()) std::getline (string)
istream& getline (istream& is, string& str, char delim)
{ istream& getline (istream& is, string& str);
getline(file, line);
Get line from stream into string, Extracts
cout << line << endl;
characters from is and stores them into str until
}
the delimitation character delim is found (or th
newline character, '\n', for (2)).
file.close();
}
the default delimiter is \n