Title: Principles of Programming I
1Principles of Programming I
2Semester Overview
- Functions
- Character File I/O
- Arrays
- Pointers and Dynamic Memory Allocation
- Characters and Strings
- Structures/Records
- Binary File I/O
3Objectives
- Structures, Records and Files
4Pre-Reqs
- Review 3 steps in dealing with a file
- open a stream to the file
- use fstream, ifstream, or ofstream
- read/write to the file
- use ltlt, gtgt, get(), getline(), etc.
- close the stream to the file
5Pre-Reqs
struct person char name20 int
age cout ltlt sizeof(person)
What would this display?
6Converting Pointers
- To convert a pointer from one type to another
type, use reinterpret_cast type cast.
General Form reinterpret_castltdataTypegt(value)
int x 10 char ptr ptr reinterpret_castltchar
gt(x)
7Binary File I/O
Binary files contain data that is unformatted and
not necessarily stored as ASCII text
What happens if you open a MS Word file in
notepad?
8Review
Character File I/O
int x 1234 ofstream out(stuff.dat) out ltlt x
This would write the ASCII characters 1, 2,
3, 4 to the file stuff.dat
9- Writing Binary
- Data to a File
ofstream out(file.dat, iosoutiosbinary)
10The Structure
Remember this structure
struct person char name20 int age
11Write
fileObject.write(address, size)
- fileObject the name of the file stream object
- address The starting address of the section of
memory that is to be written to the file.
Expected to be a char. - size The number of bytes of memory to write.
Must be an integer value
12Setup
int main() person p fstream
outFile(people.dat,
iosoutiosbinary) if(outFile.fail())
cout ltlt Error opening fileltltendl return
0 getData(p, outfile)//coming up
outFile.close() return 0
13getData
void getData(person p, fstream out) char
again int flag do char buffer80 cout
ltlt "Enter the persons name " cin.getline(p.name
, 19)
14getData
//ask for, get, and validate the age
do flag 0 cout ltlt "Age " cin.getline(buff
er, 4) for (int i 0 i lt strlen(buffer)
i) if (!isdigit(bufferi)) flag
1 cout ltlt "invalid data entered" ltlt
endl break while(flag 1)
15getData
// The good stuff p.age atoi(buffer)
out.write(reinterpret_castltchar gt(p),
sizeof(person)) cout ltlt "Do you want to
enter another ltlt record? " cin gtgt
again cin.ignore(80, '\n') while
(toupper(again) 'Y')
16- Reading Binary
- Data from a File
ofstream in(file.dat, iosiniosbinary)
17Write
fileObject.read(address, size)
- fileObject the name of the file stream object
- address The starting address of the section of
memory where the data being read from the file is
to be stored. Expected to be a char. - size The number of bytes of memory to read.
Must be an integer value.
18main
int main() fstream inFile("people.dat",
iosiniosbinary)
if(inFile.fail()) cout ltlt "Error opening
file" ltlt endl return 0
readData(inFile)//coming up inFile.close()
return 0
19readData
void readData(fstream in) person p char
again in.read(reinterpret_castltchar gt(p),
sizeof(person)) while(!in.eof())
cout ltlt "Name " ltlt p.name ltlt endl cout ltlt
"Age " ltlt p.age ltlt endl cout ltlt "\nStrike
Enter to see the next record." cin.get(again)
in.read(reinterpret_castltchar gt(p),
sizeof(person)) cout ltlt endl ltlt "That's
all!" ltlt endl
20Reading Into an Array Example
int main() fstream inFile("people.dat",
iosiniosbinary) person
pArr20 int count if(inFile.fail())
cout ltlt "Error opening file" ltlt endl return
0 readData(inFile, pArr, count)//coming
up inFile.close() return 0
21Reading Into an Array Example
void readData(fstream in, person p, int
count) count 0 in.read(reinterpret_castlt
char gt(p0), sizeof(person))
while(!in.eof()) cout ltlt pcount.name ltlt
endl count in.read(reinterpret_castltchar
gt(pcount), sizeof(person))
22 23Random Access Files
- You have the ability to set the read and write
pointers that are in the file
write
read
24seekp seekg
- 2 functions to move the read/write position in
the file - seekp used w/ files opened for output
- seekg used w/ files opened for input
25Functions for Random File Access
- file.seekp(offset, mode)
- file.seekp(20L, iosbeg)
- Sets the write position to the 21st byte from the
beginning of the file - file.seekp(-40L, iosend)
- Sets the write postion to the 40th byte from the
end of the file - file.seekp(2L, ioscur)
- Sets the write position to the 3rd byte from the
current position
26Functions for Random File Access
- file.seekg(offset, mode)
- file.seekg(20L, iosbeg)
- Sets the read position to the 21st byte from the
beginning of the file - file.seekg(-40L, iosend)
- Sets the read postion to the 40th byte from the
end of the file - file.seekg(2L, ioscur)
- Sets the read position to the 3rd byte from the
current position
27 Simple Example
int main() fstream in("letters.txt",
iosin) char ch in.seekg(5L,
iosbeg) in.get(ch) cout ltlt "Byte 5 from
beginnning " ltlt ch ltlt endl
//Contd on next slide
abcdefghijklm
28Simple Example
in.seekg(-10L, iosend) in.get(ch) cout ltlt
"Byte 10 from end " ltlt ch ltlt
endl in.seekg(3L, ioscur) in.get(ch) cout
ltlt "Byte 3 from current " ltlt ch ltlt
endl in.close() return 0
abcdefghijklm
29Random Access with Structures
struct inven char desc31 int qty float
price
30Random Access with Structures
int main() fstream inventory("inven.dat",
iosoutiosbinary) inven r "", 0,
0.0f //write 5 blank records to the
inven.dat for (int i 0 i lt 5 i)
inventory.write(reinterpret_castltchar gt(r),
sizeof(inven)) inventory.close()
return 0
Weve written 5 blank records! Yay! . Now what?
31Class Exercise
- Open stream to inven.dat
- Ask the user which record they want to change
- Find that record in the file, read it, change it,
and write it back out.
32Questions??