1 / 21

File I/O Finished off

File I/O Finished off. Colin Cherry standing in for Dr. Lin. 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

Télécharger la présentation

File I/O Finished off

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File I/O Finished off Colin Cherry standing in for Dr. Lin

  2. 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!

  3. Solutions (1): • Use the string class! • Will resize automatically if too small • Should work for most situations

  4. 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

  5. Overflow Demo! !

  6. 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()

  7. 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;

  8. Saving Flag Settings • Flag settings ‘stay’ until changed • Precision and setf flags can be savedand restored • Function precision() returns current settingif called with no arguments • Member function flags() provides similarcapability

  9. 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);}

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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);

  16. Opening for binary fstream obStream, ibStream; obstream.open(“file”, ios::binary| ios::out); // OR ibstream.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

  17. 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));

  18. 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

  19. 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

  20. To the example! !

  21. Summary • Streams connect to files with openoperation • Member function fail() checks successes • Stream member functions format output • e.g.: width, setf, precision • Same usage for cout (screen) or files • Member function eof • Used to test for end of input file

More Related