Random Access. McGraw-Hill The Complete Reference Fourth Edition
Reklama
Reklama
C h a p t e r 2 1 : C + + F i l e I / O
557 can force the information to be physically written to disk before the buffer is full by calling flush( ). Its prototype is ostream &flush( );
Calls to flush( ) might be warranted when a program is going to be used in adverse environments (for example, in situations where power outages occur frequently).
Closing a file or terminating a program also flushes all buffers.
Random Access
In C++'s I/O system, you perform random access by using the seekg( ) and seekp( ) functions. Their most common forms are istream &seekg(off_type offset, seekdir origin); ostream &seekp(off_type offset, seekdir origin);
Here, off_type is an integer type defined by ios that is capable of containing the largest valid value that offset can have. seekdir is an enumeration defined by ios that determines how the seek will take place.
The C++ I/O system manages two pointers associated with a file. One is the get
pointer, which specifies where in the file the next input operation will occur. The other is the put pointer, which specifies where in the file the next output operation will occur.
Each time an input or output operation takes place, the appropriate pointer is automatically sequentially advanced. However, using the seekg( ) and seekp( ) functions allows you to access the file in a nonsequential fashion.
The seekg( ) function moves the associated file's current get pointer offset number of characters from the specified origin, which must be one of these three values: ios::beg ios::cur ios::end
Beginning-of-file
Current location
End-of-file
The seekp( ) function moves the associated file's current put pointer offset number of characters from the specified origin, which must be one of the values just shown.
Generally, random-access I/O should be performed only on those files opened for binary operations. The character translations that may occur on text files could cause a position request to be out of sync with the actual contents of the file.
558
C + + : T h e C o m p l e t e R e f e r e n c e
The following program demonstrates the seekp( ) function. It allows you to change a specific character in a file. Specify a filename on the command line, followed by the number of the character in the file you want to change, followed by the new character.
Notice that the file is opened for read/write operations.
#include <iostream>
#include <fstream>
#include <cstdlib> using namespace std; int main(int argc, char *argv[])
{ if(argc!=4) { cout << "Usage: CHANGE <filename> <character> <char>\n"; return 1;
} fstream out(argv[1], ios::in | ios::out | ios::binary); if(!out) { cout << "Cannot open file."; return 1;
} out.seekp(atoi(argv[2]), ios::beg); out.put(*argv[3]); out.close();
} return 0;
For example, to use this program to change the twelfth character of a file called
TEST to a Z, use this command line: change test 12 Z
The next program uses seekg( ). It displays the contents of a file beginning with the location you specify on the command line.
#include <iostream>
#include <fstream>
C h a p t e r 2 1 : C + + F i l e I / O
559
#include <cstdlib> using namespace std; int main(int argc, char *argv[])
{ char ch; if(argc!=3) { cout << "Usage: SHOW <filename> <starting location>\n"; return 1;
} ifstream in(argv[1], ios::in | ios::binary); if(!in) { cout << "Cannot open file."; return 1;
} in.seekg(atoi(argv[2]), ios::beg); while(in.get(ch)) cout << ch;
} return 0;
The following program uses both seekp( ) and seekg( ) to reverse the first <num> characters in a file.
#include <iostream>
#include <fstream>
#include <cstdlib> using namespace std; int main(int argc, char *argv[])
{ if(argc!=3) { cout << "Usage: Reverse <filename> <num>\n"; return 1;
}
560
C + + : T h e C o m p l e t e R e f e r e n c e fstream inout(argv[1], ios::in | ios::out | ios::binary); if(!inout) { cout << "Cannot open input file.\n"; return 1;
} long e, i, j; char c1, c2; e = atol(argv[2]); for(i=0, j=e; i<j; i++, j--) { inout.seekg(i, ios::beg); inout.get(c1); inout.seekg(j, ios::beg); inout.get(c2);
} inout.seekp(i, ios::beg); inout.put(c2); inout.seekp(j, ios::beg); inout.put(c1);
} inout.close(); return 0;
To use the program, specify the name of the file that you want to reverse, followed by the number of characters to reverse. For example, to reverse the first 10 characters of a file called TEST, use this command line: reverse test 10
If the file had contained this:
This is a test.
it will contain the following after the program executes: a si sihTtest.
Stáhnout
Reklama