Post on 18-Dec-2015
transcript
What happens if your input is bigger than your character array?
char arr[10]; cin >> arr; The characters are written to memory starting
at the spot pointed to by arr Depending on whether or the memory is
being used, different things could happen:– Harmless– Could be used to execute malicious code– Segfault!
Solutions (1):
Use the string class! Will resize automatically if too small Should work for most situations
Solutions (2):
Use cin.getline(arr,size) Will read in up to size-1 characters Will add the c-string delimiter for you Will fail if buffer overflows, but gracefully
– Have to check cin.fail() Good for situations where an agreement
exists– Any violation is undefined
Stream object flags
When a file operation fails or hits EOF, it sets a flag– Two from our example, fin.fail(), fin.eof()
These flags stay with the stream object– fin.eof() returns true, even after we close
the current file and open a new one– To reset stream object, call clear()
So where were we?
Went over opening & closing files Using the << and >> for input & output Various stream flags and options:
– Precision, width, setf Manipulators
– cout << setw(6) << myNumber << endl;
Saving Flag Settings Flag settings ‘stay’ until changed Precision and setf flags can be saved
and restored Function precision() returns current setting
if called with no arguments Member function flags() provides similar
capability
Saving Flag Settings Example void outputStuff(ofstream& outStream)
{int precisionSetting =
outStream.precision();long flagSettings = outStream.flags();outStream.setf(ios::fixed | ios::showpoint);outStream.precision(2);
outStream << “Do stuff here!”;
outStream.precision(precisionSetting);outStream.flags(flagSettings);
}
Restoring Default setf Settings Can also restore default settings:
cout.setf(0, ios::floatfield); Not necessarily the ‘last’ setting! Default values are implementation-
dependent Does not reset precision settings
Only setf settings
A function that takes a stream
void outputStuff(ofstream& outStream){}
But this also works
Void outputStuff(ostream& outStream) Stream always need to be passed by
reference
Stream Hierarchies
Input file streams class is derived from classof all input streams It then adds open and close member functions
ifstream is derived from istream ofstream is derived from ostream
Random Access to Files Sequential Access
Most commonly used Random Access
Rapid access to records Perhaps very large database Access ‘randomly’ to any part of file Use fstream objects
input and output
Random Access Tools Opens same as istream or ostream
Adds second argument fstream rwStream;
rwStream.open(“stuff”, ios::in | ios:: out); Opens with read and write capability
Move about in file rwStream.seekp(1000);
Positions put-pointer at 1000th byte
rwStream.seekg(1000); Positions get-pointer at 1000th byte
Random Access Sizes To move about must know sizes
sizeof() operator determines number of bytesrequired for an object:sizeof(s) //Where s is string s = “Hello”sizeof(10)sizeof(double)sizeof(myObject)
Position put-pointer at 100th record of objects:
rwStream.seekp(100*sizeof(myObject) – 1);
Opening for binary
fstream obStream, ibStream;obstream.open(“file”, ios::binary| ios::out);// ORibstream.open(“file”, ios::binaray | ios::in); ios::binary, ios::in and ios::out
– Other constants that tell C++ how to open files
– Like ios::app
Writing bytes
Characters are one byte each, so you’ve been doing it all along!
You can write other things, though:– Base data types (int, float)– Classes and structs– Arrays
Just cast to character first obstream.write( (char*) obj, sizeof(obj));
Why would I do that?
Say you were writing software for a video store– Each movie is a class with title, running time,
status (rented or no), and rented by field– Inventory is an array of classes– Just write inventory to file, makes it easier to save
and load before and after shut-down Why not just build over a database?
– Well, if you want to do it the easy way, sure…– Valid example: Dr. Lin’s load on demand hash
table
Reading bytes
Same deal as before:– ibstream.read( (char*) &obj, sizeof(obj));– Make sure your object is a pointer!
Tricky bit:– You need to know the size of an object in advance
Solutions:– Fixed width objects (easy, not always possible)– Header