Binary File IO
Binary File IO
165 244 56 4 …
…
…
… … … … …
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Opening a File Stream C++ Binary File I/O 2
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
File Pointers C++ Binary File I/O 3
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Reading Data C++ Binary File I/O 4
#include <iostream>
#include <fstream>
void main() {
char buffer[100];
ifstream myFile (“data.bin”, ios::in | ios::binary);
myFile.read(buffer,100);
if (!myFile) { //error occurred
cerr << “Error reading from file data.bin, only “
<< myFile.gcount() << “ bytes read” << endl;
myFile.clear(); //reset file for reading
}
}
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Writing Data C++ Binary File I/O 5
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Example: Text to Binary C++ Binary File I/O 6
void main() {
string Name;
int* Scores = new int[5];
int NameLen;
ifstream TextIn("Data.txt");
ofstream BinOut("Data.bin", ios::out | ios::binary);
NameLen = Name.length();
BinOut.write((char*) &NameLen, sizeof(int)); Convert string
object to char*
BinOut.write(Name.c_str(), Name.length());
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Example: Binary to Text C++ Binary File I/O 7
while (BinIn) {
BinIn.read(cName, NameLen);
cName[NameLen] = '\0';
BinIn.close();
TextOut.close();
}
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Example: Input and Results C++ Binary File I/O 8
Kernighan, Brian 48 -1 -1 -1 -1
Gates, Bill 101 22 -1 -1 -1
Jobs, Steve 46 -1 -1 -1 -1
Stroustrup, Bjarne 21 86 52 93 -1
Lovelace, Ada 57 91 13 54 -1
von Neumann, Jon 7 84 -1 -1 -1
Kernighan, Brian:48:-1:-1:-1:-1
Gates, Bill:101:22:-1:-1:-1
Jobs, Steve:46:-1:-1:-1:-1
Stroustrup, Bjarne:21:86:52:93:-1
Lovelace, Ada:57:91:13:54:-1
von Neumann, Jon:7:84:-1:-1:-1
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Example: Binary Output File C++ Binary File I/O 9
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Moving File Pointers C++ Binary File I/O 10
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Reading and Writing Complex Data C++ Binary File I/O 11
#include <fstream>
class Data {
public:
Data() : key(0), address(0) {}
Data(int k, unsigned long add) : key(k), address(add) {}
int getKey() const { return key; }
unsigned long getAddress() const { return address; }
private:
int key; However, be careful of this if the "object"
unsigned long address; has dynamic content; e.g.,
}
class Place {
private:
string Name;
. . .
};
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Complex Data (cont) C++ Binary File I/O 12
void main() {
Data entry(1,200L);
Data *array = new Data[10];
unsigned long location1 = 10L*sizeof(Data);
fstream myFile(“data.bin”,
ios::in | ios::out | ios::binary);
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Dynamic Objects C++ Binary File I/O 13
Simply writing the correct number of bytes, from the starting address of the
object in memory, will NOT produce the desired result.
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Data Class C++ Binary File I/O 14
return (Name.length() +
State.length() +
sizeof(int));
}
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Index Entry Class C++ Binary File I/O 15
We also employ a simple class to hold the file index data for a City object:
class Entry {
private:
string Key;
unsigned int Address;
public:
Entry();
Entry(string K, unsigned int A);
string getKey() const;
unsigned int getAddress() const;
};
We assume that city names are unique, and use those as our primary key. No
hashing is employed; the Entry objects are simply stored in the order that the
City objects are written to the file.
That is not ideal.
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Writing a City Object to the DB File C++ Binary File I/O 16
Writing the data fields of a City object to the current DB file location is
relatively simple:
void writeCity(ostream& Out, City* toWrite) {
To recover the strings we must either store length data (as shown here) or write
delimiters to the DB file.
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Reading a City Object from the DB File C++ Binary File I/O 17
Reading the data fields and reconstructing a City object from the current DB
file location is also relatively simple:
City readCity(istream& In) {
int NameLen, StateLen, Population;
delete [] cName;
delete [] cState;
return toReturn;
}
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Building an Index for the DB File C++ Binary File I/O 18
A text file of city data is parsed, and an array of City objects is created. Then,
those City objects are written to a binary DB file and an array of index Entry
objects is created:
City G[MAXCITIES];
Entry H[MAXCITIES];
fstream BinData("Data.bin",
ios::in | ios::out | ios::binary);
writeCity(BinData, &G[Idx]);
}
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD
Finding a City Object in the DB File C++ Binary File I/O 19
Finding a city, given its name, is a simple matter of looking up the file offset for
the relevant record and then reading in that record:
int Idx = 0;
while ( (Idx < Size) && (Index[Idx].getKey() != CityName) ) {
Idx++;
}
if (Idx == Size) {
return City("No such city", "", 0);
}
unsigned int Offset = Index[Idx].getAddress();
DB.seekg(0);
DB.seekg(Offset);
City Target = readCity(DB);
return Target;
}
Computer Science Dept Va Tech October 2000 Data Structures & File Management ©2000 Keller, B & McQuain WD